Class HashCollections


  • public final class HashCollections
    extends Object
    Static factory methods to construct HashMaps/HashSets based on alternatives to Java native implementations, which alternatives must support:
    1. Immutable HashMap/HashSet implementations. By default, Guava ImmutableMap/ImmutableSet factory is used.
    2. Mutable HashMap/HashSet implementations accepting an expected size as constructor parameter. By default, Guava Maps/Sets factory is used.
    Ideally, the alternative should support updatable HashMaps/HashSets accepting an expected size as constructor parameter as well. (Guava does not provide such implementation so the mutable variant from Maps/Sets is used by default instead.)

    See Also:
    "http://java-performance.info/hashmap-overview-jdk-fastutil-goldman-sachs-hppc-koloboke-trove-january-2015/"
    • Constructor Summary

      Constructors 
      Constructor Description
      HashCollections()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <K,​V>
      Map<K,​V>
      newImmutableMap​(Map<? extends K,​? extends V> map)
      Constructs a new immutable map with the same mappings as the specified map.
      static <K,​V>
      Map<K,​V>
      newImmutableMap​(Map<? extends K,​? extends V> map1, Map<? extends K,​? extends V> map2)
      Constructs a new immutable map which merges the mappings of the specified maps.
      static <K,​V>
      Map<K,​V>
      newImmutableMap​(K k1, V v1)
      Constructs a new immutable map of the single specified mapping.
      static <K,​V>
      Map<K,​V>
      newImmutableMap​(K k1, V v1, K k2, V v2)
      Constructs a new immutable map of the two specified mappings.
      static <K,​V>
      Map<K,​V>
      newImmutableMap​(K k1, V v1, K k2, V v2, K k3, V v3)
      Constructs a new immutable map of the three specified mappings.
      static <E> Set<E> newImmutableSet​(E e1)
      Constructs a new immutable singleton set of the given element.
      static <E> Set<E> newImmutableSet​(E[] elements)
      Constructs a new immutable set of elements from the given array.
      static <E> Set<E> newImmutableSet​(Iterable<? extends E> elements)
      Constructs a new immutable set containing the elements in the specified iterable.
      static <E> Set<E> newImmutableSet​(Set<? extends E> set1, Set<? extends E> set2)
      Constructs a new immutable set which merges the elements of the specified sets.
      static <K,​V>
      Map<K,​V>
      newMutableMap()
      Constructs a new empty mutable map of the default expected size (depending on the underlying implementation).
      static <K,​V>
      Map<K,​V>
      newUpdatableMap()
      Constructs a new empty updatable map of the default expected size (depending on the underlying implementation).
      static <K,​V>
      Map<K,​V>
      newUpdatableMap​(int expectedSize)
      Constructs a new empty updatable map of the given expected size.
      static <K,​V>
      Map<K,​V>
      newUpdatableMap​(Map<? extends K,​? extends V> map)
      Constructs a new updatable map with the same mappings as the specified map.
      static <K,​V>
      Map<K,​V>
      newUpdatableMap​(Map<? extends K,​? extends V> map1, Map<? extends K,​? extends V> map2)
      Constructs a new updatable map which merge the mappings of the specified maps.
      static <K,​V>
      Map<K,​V>
      newUpdatableMap​(Map<? extends K,​? extends V> map1, Map<? extends K,​? extends V> map2, Map<? extends K,​? extends V> map3)
      Constructs a new updatable map which merge the mappings of the specified maps.
      static <E> Set<E> newUpdatableSet()
      Constructs a new empty updatable set of the default expected size (depending on the underlying implementation).
      static <E> Set<E> newUpdatableSet​(int expectedSize)
      Constructs a new empty updatable set of the given expected size.
      static <E> Set<E> newUpdatableSet​(Iterable<? extends E> elements)
      Constructs a new updatable set containing the elements in the specified iterable.
    • Constructor Detail

      • HashCollections

        public HashCollections()
    • Method Detail

      • newMutableMap

        public static <K,​V> Map<K,​V> newMutableMap()
        Constructs a new empty mutable map of the default expected size (depending on the underlying implementation).
        Returns:
        a new empty mutable map
      • newUpdatableMap

        public static <K,​V> Map<K,​V> newUpdatableMap()
        Constructs a new empty updatable map of the default expected size (depending on the underlying implementation).
        Returns:
        new empty updatable map
      • newUpdatableMap

        public static <K,​V> Map<K,​V> newUpdatableMap​(int expectedSize)
        Constructs a new empty updatable map of the given expected size.
        Parameters:
        expectedSize - expected size of the returned map
        Returns:
        a new empty updatable map of the given expected size
      • newUpdatableMap

        public static <K,​V> Map<K,​V> newUpdatableMap​(Map<? extends K,​? extends V> map)
        Constructs a new updatable map with the same mappings as the specified map.
        Parameters:
        map - the map whose mappings are to be placed in the returned map
        Returns:
        a new updatable map with the same mappings as the specified map
      • newUpdatableMap

        public static <K,​V> Map<K,​V> newUpdatableMap​(Map<? extends K,​? extends V> map1,
                                                                 Map<? extends K,​? extends V> map2)
        Constructs a new updatable map which merge the mappings of the specified maps. On conflict, mappings from the map2 have priority over mappings from the map1 with the same keys.
        Parameters:
        map1 - the first map to merge
        map2 - the second map to merge
        Returns:
        a new updatable map which merges the mappings of the specified maps
      • newUpdatableMap

        public static <K,​V> Map<K,​V> newUpdatableMap​(Map<? extends K,​? extends V> map1,
                                                                 Map<? extends K,​? extends V> map2,
                                                                 Map<? extends K,​? extends V> map3)
        Constructs a new updatable map which merge the mappings of the specified maps. On conflict, mappings from the maps passed later in the argument list have priority over mappings from the maps passed earlier with the same keys.
        Parameters:
        map1 - the first map to merge
        map2 - the second map to merge
        map3 - the third map to merge
        Returns:
        a new updatable map which merges the mappings of the specified maps
      • newImmutableMap

        public static <K,​V> Map<K,​V> newImmutableMap​(Map<? extends K,​? extends V> map)
        Constructs a new immutable map with the same mappings as the specified map.
        Parameters:
        map - the map whose mappings are to be placed in the returned map
        Returns:
        a new immutable map with the same mappings as the specified map
      • newImmutableMap

        public static <K,​V> Map<K,​V> newImmutableMap​(Map<? extends K,​? extends V> map1,
                                                                 Map<? extends K,​? extends V> map2)
        Constructs a new immutable map which merges the mappings of the specified maps. On conflict, mappings from the map2 have priority over mappings from the map1 with the same keys.
        Parameters:
        map1 - the first map to merge
        map2 - the second map to merge
        Returns:
        a new immutable map which merges the mappings of the specified maps
      • newImmutableMap

        public static <K,​V> Map<K,​V> newImmutableMap​(K k1,
                                                                 V v1)
        Constructs a new immutable map of the single specified mapping.
        Parameters:
        k1 - the key of the sole mapping
        v1 - the value of the sole mapping
        Returns:
        a new immutable map of the single specified mapping
      • newImmutableMap

        public static <K,​V> Map<K,​V> newImmutableMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2)
        Constructs a new immutable map of the two specified mappings.
        Parameters:
        k1 - the key of the first mapping
        v1 - the value of the first mapping
        k2 - the key of the second mapping
        v2 - the value of the second mapping
        Returns:
        Returns an immutable map containing the given entries, in order.
      • newImmutableMap

        public static <K,​V> Map<K,​V> newImmutableMap​(K k1,
                                                                 V v1,
                                                                 K k2,
                                                                 V v2,
                                                                 K k3,
                                                                 V v3)
        Constructs a new immutable map of the three specified mappings.
        Parameters:
        k1 - the key of the first mapping
        v1 - the value of the first mapping
        k2 - the key of the second mapping
        v2 - the value of the second mapping
        k3 - the key of the third mapping
        v3 - the value of the third mapping
        Returns:
        Returns an immutable map containing the given entries, in order.
      • newUpdatableSet

        public static <E> Set<E> newUpdatableSet()
        Constructs a new empty updatable set of the default expected size (depending on the underlying implementation).
        Returns:
        a new empty updatable set
      • newUpdatableSet

        public static <E> Set<E> newUpdatableSet​(int expectedSize)
        Constructs a new empty updatable set of the given expected size.
        Parameters:
        expectedSize - the expected size of the returned set
        Returns:
        a new empty updatable set of the given expected size
      • newUpdatableSet

        public static <E> Set<E> newUpdatableSet​(Iterable<? extends E> elements)
        Constructs a new updatable set containing the elements in the specified iterable.
        Parameters:
        elements - the iterable whose elements are to be placed into the returned set
        Returns:
        a new updatable set of the elements of the specified iterable
      • newImmutableSet

        public static <E> Set<E> newImmutableSet​(E[] elements)
        Constructs a new immutable set of elements from the given array.
        Parameters:
        elements - the array whose elements are to be placed into the returned set
        Returns:
        a new immutable set of elements from the given array
      • newImmutableSet

        public static <E> Set<E> newImmutableSet​(Set<? extends E> set1,
                                                 Set<? extends E> set2)
        Constructs a new immutable set which merges the elements of the specified sets.
        Parameters:
        set1 - the first source of elements for the returned set
        set2 - the second source of elements for the returned set
        Returns:
        a new immutable set which merges the elements of the specified sets
      • newImmutableSet

        public static <E> Set<E> newImmutableSet​(Iterable<? extends E> elements)
        Constructs a new immutable set containing the elements in the specified iterable.
        Parameters:
        elements - the iterable whose elements are to be placed into the returned set
        Returns:
        a new immutable set of the elements of the specified iterable
      • newImmutableSet

        public static <E> Set<E> newImmutableSet​(E e1)
        Constructs a new immutable singleton set of the given element.
        Parameters:
        e1 - the sole element
        Returns:
        a new immutable singleton set of the given element