Class UnifiedMap<K,​V>

  • All Implemented Interfaces:
    java.io.Externalizable, java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<V>, java.util.Map<K,​V>, InternalIterable<V>, MapIterable<K,​V>, MutableMap<K,​V>, MutableMapIterable<K,​V>, UnsortedMapIterable<K,​V>, RichIterable<V>, BatchIterable<V>

    public class UnifiedMap<K,​V>
    extends AbstractMutableMap<K,​V>
    implements java.io.Externalizable, BatchIterable<V>
    UnifiedMap stores key/value pairs in a single array, where alternate slots are keys and values. This is nicer to CPU caches as consecutive memory addresses are very cheap to access. Entry objects are not stored in the table like in java.util.HashMap. Instead of trying to deal with collisions in the main array using Entry objects, we put a special object in the key slot and put a regular Object[] in the value slot. The array contains the key value pairs in consecutive slots, just like the main array, but it's a linear list with no hashing.

    The final result is a Map implementation that's leaner than java.util.HashMap and faster than Trove's THashMap. The best of both approaches unified together, and thus the name UnifiedMap.

    See Also:
    Serialized Form
    • Field Detail

      • NULL_KEY

        protected static final java.lang.Object NULL_KEY
      • CHAINED_KEY

        protected static final java.lang.Object CHAINED_KEY
      • DEFAULT_INITIAL_CAPACITY

        protected static final int DEFAULT_INITIAL_CAPACITY
        See Also:
        Constant Field Values
      • table

        protected transient java.lang.Object[] table
      • occupied

        protected transient int occupied
      • loadFactor

        protected float loadFactor
      • maxSize

        protected int maxSize
    • Constructor Detail

      • UnifiedMap

        public UnifiedMap()
      • UnifiedMap

        public UnifiedMap​(int initialCapacity)
      • UnifiedMap

        public UnifiedMap​(int initialCapacity,
                          float loadFactor)
      • UnifiedMap

        public UnifiedMap​(java.util.Map<? extends K,​? extends V> map)
      • UnifiedMap

        public UnifiedMap​(Pair<K,​V>... pairs)
    • Method Detail

      • newMap

        public static <K,​V> UnifiedMap<K,​V> newMap()
      • newMap

        public static <K,​V> UnifiedMap<K,​V> newMap​(int size)
      • newMap

        public static <K,​V> UnifiedMap<K,​V> newMap​(int size,
                                                               float loadFactor)
      • newMap

        public static <K,​V> UnifiedMap<K,​V> newMap​(java.util.Map<? extends K,​? extends V> map)
      • newMapWith

        public static <K,​V> UnifiedMap<K,​V> newMapWith​(Pair<K,​V>... pairs)
      • newMapWith

        public static <K,​V> UnifiedMap<K,​V> newMapWith​(java.lang.Iterable<Pair<K,​V>> inputIterable)
      • newWithKeysValues

        public static <K,​V> UnifiedMap<K,​V> newWithKeysValues​(K key,
                                                                          V value)
      • newWithKeysValues

        public static <K,​V> UnifiedMap<K,​V> newWithKeysValues​(K key1,
                                                                          V value1,
                                                                          K key2,
                                                                          V value2)
      • newWithKeysValues

        public static <K,​V> UnifiedMap<K,​V> newWithKeysValues​(K key1,
                                                                          V value1,
                                                                          K key2,
                                                                          V value2,
                                                                          K key3,
                                                                          V value3)
      • newWithKeysValues

        public static <K,​V> UnifiedMap<K,​V> newWithKeysValues​(K key1,
                                                                          V value1,
                                                                          K key2,
                                                                          V value2,
                                                                          K key3,
                                                                          V value3,
                                                                          K key4,
                                                                          V value4)
      • withKeysValues

        public UnifiedMap<K,​V> withKeysValues​(K key,
                                                    V value)
      • withKeysValues

        public UnifiedMap<K,​V> withKeysValues​(K key1,
                                                    V value1,
                                                    K key2,
                                                    V value2)
      • withKeysValues

        public UnifiedMap<K,​V> withKeysValues​(K key1,
                                                    V value1,
                                                    K key2,
                                                    V value2,
                                                    K key3,
                                                    V value3)
      • withKeysValues

        public UnifiedMap<K,​V> withKeysValues​(K key1,
                                                    V value1,
                                                    K key2,
                                                    V value2,
                                                    K key3,
                                                    V value3,
                                                    K key4,
                                                    V value4)
      • fastCeil

        private int fastCeil​(float v)
      • init

        protected int init​(int initialCapacity)
      • allocate

        protected int allocate​(int capacity)
      • allocateTable

        protected void allocateTable​(int sizeToAllocate)
      • computeMaxSize

        protected void computeMaxSize​(int capacity)
      • index

        protected int index​(java.lang.Object key)
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
      • put

        public V put​(K key,
                     V value)
        Specified by:
        put in interface java.util.Map<K,​V>
      • chainedPut

        private V chainedPut​(K key,
                             int index,
                             V value)
      • chainedUpdateValue

        private V chainedUpdateValue​(K key,
                                     int index,
                                     Function0<? extends V> factory,
                                     Function<? super V,​? extends V> function)
      • chainedUpdateValueWith

        private <P> V chainedUpdateValueWith​(K key,
                                             int index,
                                             Function0<? extends V> factory,
                                             Function2<? super V,​? super P,​? extends V> function,
                                             P parameter)
      • chainedGetIfAbsentPut

        private V chainedGetIfAbsentPut​(K key,
                                        int index,
                                        Function0<? extends V> function)
      • chainedGetIfAbsentPut

        private V chainedGetIfAbsentPut​(K key,
                                        int index,
                                        V value)
      • getIfAbsentPutWith

        public <P> V getIfAbsentPutWith​(K key,
                                        Function<? super P,​? extends V> function,
                                        P parameter)
        Description copied from interface: MutableMapIterable
        Get and return the value in the Map at the specified key. Alternatively, if there is no value in the map for that key return the result of evaluating the specified Function using the specified parameter, and put that value in the map at the specified key.
        Specified by:
        getIfAbsentPutWith in interface MutableMapIterable<K,​V>
        Overrides:
        getIfAbsentPutWith in class AbstractMutableMapIterable<K,​V>
      • chainedGetIfAbsentPutWith

        private <P> V chainedGetIfAbsentPutWith​(K key,
                                                int index,
                                                Function<? super P,​? extends V> function,
                                                P parameter)
      • getCollidingBuckets

        public int getCollidingBuckets()
      • getMapMemoryUsedInWords

        public int getMapMemoryUsedInWords()
        Returns the number of JVM words that is used by this map. A word is 4 bytes in a 32bit VM and 8 bytes in a 64bit VM. Each array has a 2 word header, thus the formula is: words = (internal table length + 2) + sum (for all chains (chain length + 2))
        Returns:
        the number of JVM words that is used by this map.
      • rehash

        protected void rehash​(int newCapacity)
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
        Specified by:
        get in interface MapIterable<K,​V>
        See Also:
        Map.get(Object)
      • getFromChain

        private V getFromChain​(java.lang.Object[] chain,
                               K key)
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Specified by:
        containsKey in interface MapIterable<K,​V>
        See Also:
        Map.containsKey(Object)
      • chainContainsKey

        private boolean chainContainsKey​(java.lang.Object[] chain,
                                         K key)
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Specified by:
        containsValue in interface MapIterable<K,​V>
        See Also:
        Map.containsValue(Object)
      • chainedContainsValue

        private boolean chainedContainsValue​(java.lang.Object[] chain,
                                             V value)
      • forEachKeyValue

        public void forEachKeyValue​(Procedure2<? super K,​? super V> procedure)
        Description copied from interface: MapIterable
        Calls the procedure with each key-value pair of the map.
             final Collection<String> collection = new ArrayList<String>();
             MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three");
             map.forEachKeyValue((Integer key, String value) -> collection.add(String.valueOf(key) + value));
             Verify.assertContainsAll(collection, "1One", "2Two", "3Three");
         
        Specified by:
        forEachKeyValue in interface MapIterable<K,​V>
      • getFirst

        public V getFirst()
        Description copied from interface: RichIterable
        Returns the first element of an iterable. In the case of a List it is the element at the first index. In the case of any other Collection, it is the first element that would be returned during an iteration. If the iterable is empty, null is returned. If null is a valid element of the container, then a developer would need to check to see if the iterable is empty to validate that a null result was not due to the container being empty.

        The order of Sets are not guaranteed (except for TreeSets and other Ordered Set implementations), so if you use this method, the first element could be any element from the Set.

        Specified by:
        getFirst in interface RichIterable<K>
        Overrides:
        getFirst in class AbstractMapIterable<K,​V>
      • collectKeysAndValues

        public <E> MutableMap<K,​V> collectKeysAndValues​(java.lang.Iterable<E> iterable,
                                                              Function<? super E,​? extends K> keyFunction,
                                                              Function<? super E,​? extends V> valueFunction)
        Description copied from interface: MutableMap
        Adds all the entries derived from iterable to this. The key and value for each entry is determined by applying the keyFunction and valueFunction to each item in collection. Any entry in map that has the same key as an entry in this will have its value replaced by that in map.
        Specified by:
        collectKeysAndValues in interface MutableMap<K,​V>
      • removeKey

        public V removeKey​(K key)
        Description copied from interface: MutableMapIterable
        Remove an entry from the map at the specified key.
        Specified by:
        removeKey in interface MutableMapIterable<K,​V>
        Returns:
        The value removed from entry at key, or null if not found.
        See Also:
        Map.remove(Object)
      • removeIf

        public boolean removeIf​(Predicate2<? super K,​? super V> predicate)
        Description copied from interface: MutableMapIterable
        Remove an entry from the map if the predicate evaluates to true.
        Specified by:
        removeIf in interface MutableMapIterable<K,​V>
        Returns:
        true if any entry is removed.
      • chainedForEachEntry

        private void chainedForEachEntry​(java.lang.Object[] chain,
                                         Procedure2<? super K,​? super V> procedure)
      • forEachKey

        public void forEachKey​(Procedure<? super K> procedure)
        Description copied from interface: MapIterable
        Calls the procedure with each key of the map.
             final Collection<Integer> result = new ArrayList<Integer>();
             MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "1", 2, "2", 3, "3");
             map.forEachKey(new CollectionAddProcedure<Integer>(result));
             Verify.assertContainsAll(result, 1, 2, 3);
         
        Specified by:
        forEachKey in interface MapIterable<K,​V>
        Overrides:
        forEachKey in class AbstractMapIterable<K,​V>
      • chainedForEachKey

        private void chainedForEachKey​(java.lang.Object[] chain,
                                       Procedure<? super K> procedure)
      • forEachValue

        public void forEachValue​(Procedure<? super V> procedure)
        Description copied from interface: MapIterable
        Calls the procedure with each value of the map.
             Set<String> result = UnifiedSet.newSet();
             MutableMap<Integer, String> map = this.newMapWithKeysValues(1, "One", 2, "Two", 3, "Three", 4, "Four");
             map.forEachValue(new CollectionAddProcedure<String>(result));
             Verify.assertSetsEqual(UnifiedSet.newSetWith("One", "Two", "Three", "Four"), result);
         
        Specified by:
        forEachValue in interface MapIterable<K,​V>
        Overrides:
        forEachValue in class AbstractMapIterable<K,​V>
      • chainedForEachValue

        private void chainedForEachValue​(java.lang.Object[] chain,
                                         Procedure<? super V> procedure)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> map)
        Specified by:
        putAll in interface java.util.Map<K,​V>
      • getEntrySetFrom

        private java.util.Set<? extends java.util.Map.Entry<? extends K,​? extends V>> getEntrySetFrom​(java.util.Map<? extends K,​? extends V> map)
      • copyMap

        protected void copyMap​(UnifiedMap<K,​V> unifiedMap)
      • copyChain

        private void copyChain​(java.lang.Object[] chain)
      • remove

        public V remove​(java.lang.Object key)
        Specified by:
        remove in interface java.util.Map<K,​V>
      • removeFromChain

        private V removeFromChain​(java.lang.Object[] chain,
                                  K key,
                                  int index)
      • overwriteWithLastElementFromChain

        private void overwriteWithLastElementFromChain​(java.lang.Object[] chain,
                                                       int index,
                                                       int i)
      • size

        public int size()
        Description copied from interface: RichIterable
        Returns the number of items in this iterable.
        Specified by:
        size in interface BatchIterable<K>
        Specified by:
        size in interface java.util.Map<K,​V>
        Specified by:
        size in interface RichIterable<K>
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Specified by:
        entrySet in interface java.util.Map<K,​V>
      • keySet

        public java.util.Set<K> keySet()
        Specified by:
        keySet in interface java.util.Map<K,​V>
      • values

        public java.util.Collection<V> values()
        Specified by:
        values in interface java.util.Map<K,​V>
      • equals

        public boolean equals​(java.lang.Object object)
        Description copied from interface: MapIterable
        Follows the same general contract as Map.equals(Object).
        Specified by:
        equals in interface java.util.Map<K,​V>
        Specified by:
        equals in interface MapIterable<K,​V>
        Overrides:
        equals in class java.lang.Object
      • chainedEquals

        private boolean chainedEquals​(java.lang.Object[] chain,
                                      java.util.Map<?,​?> other)
      • hashCode

        public int hashCode()
        Description copied from interface: MapIterable
        Follows the same general contract as Map.hashCode().
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Specified by:
        hashCode in interface MapIterable<K,​V>
        Overrides:
        hashCode in class java.lang.Object
      • chainedHashCode

        private int chainedHashCode​(java.lang.Object[] chain)
      • toString

        public java.lang.String toString()
        Description copied from class: AbstractRichIterable
        Returns a string with the elements of the iterable separated by commas with spaces and enclosed in square brackets.
         Assert.assertEquals("[]", Lists.mutable.empty().toString());
         Assert.assertEquals("[1]", Lists.mutable.with(1).toString());
         Assert.assertEquals("[1, 2, 3]", Lists.mutable.with(1, 2, 3).toString());
         
        Specified by:
        toString in interface MapIterable<K,​V>
        Specified by:
        toString in interface RichIterable<K>
        Overrides:
        toString in class AbstractRichIterable<V>
        Returns:
        a string representation of this collection.
        See Also:
        AbstractCollection.toString()
      • trimToSize

        public boolean trimToSize()
      • putForTrim

        private void putForTrim​(K key,
                                V value,
                                int oldIndex,
                                int mask)
      • chainedPutForTrim

        private void chainedPutForTrim​(K key,
                                       int index,
                                       V value)
      • readExternal

        public void readExternal​(java.io.ObjectInput in)
                          throws java.io.IOException,
                                 java.lang.ClassNotFoundException
        Specified by:
        readExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException
      • writeExternal

        public void writeExternal​(java.io.ObjectOutput out)
                           throws java.io.IOException
        Specified by:
        writeExternal in interface java.io.Externalizable
        Throws:
        java.io.IOException
      • writeExternalChain

        private void writeExternalChain​(java.io.ObjectOutput out,
                                        java.lang.Object[] chain)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • forEachWithIndex

        public void forEachWithIndex​(ObjectIntProcedure<? super V> objectIntProcedure)
        Description copied from interface: InternalIterable
        Iterates over the iterable passing each element and the current relative int index to the specified instance of ObjectIntProcedure.

        Example using a Java 8 lambda:

         people.forEachWithIndex((Person person, int index) -> LOGGER.info("Index: " + index + " person: " + person.getName()));
         

        Example using an anonymous inner class:

         people.forEachWithIndex(new ObjectIntProcedure<Person>()
         {
             public void value(Person person, int index)
             {
                 LOGGER.info("Index: " + index + " person: " + person.getName());
             }
         });
         
        Specified by:
        forEachWithIndex in interface InternalIterable<K>
        Overrides:
        forEachWithIndex in class AbstractMapIterable<K,​V>
      • chainedForEachValueWithIndex

        private int chainedForEachValueWithIndex​(java.lang.Object[] chain,
                                                 ObjectIntProcedure<? super V> objectIntProcedure,
                                                 int index)
      • forEachWith

        public <P> void forEachWith​(Procedure2<? super V,​? super P> procedure,
                                    P parameter)
        Description copied from interface: InternalIterable
        The procedure2 is evaluated for each element in the iterable with the specified parameter provided as the second argument.

        Example using a Java 8 lambda:

         people.forEachWith((Person person, Person other) ->
             {
                 if (person.isRelatedTo(other))
                 {
                      LOGGER.info(person.getName());
                 }
             }, fred);
         

        Example using an anonymous inner class:

         people.forEachWith(new Procedure2<Person, Person>()
         {
             public void value(Person person, Person other)
             {
                 if (person.isRelatedTo(other))
                 {
                      LOGGER.info(person.getName());
                 }
             }
         }, fred);
         
        Specified by:
        forEachWith in interface InternalIterable<K>
        Overrides:
        forEachWith in class AbstractMapIterable<K,​V>
      • chainedForEachValueWith

        private <P> void chainedForEachValueWith​(java.lang.Object[] chain,
                                                 Procedure2<? super V,​? super P> procedure,
                                                 P parameter)
      • detect

        public Pair<K,​V> detect​(Predicate2<? super K,​? super V> predicate)
        Description copied from interface: MapIterable
        Return the first key and value of the map for which the predicate evaluates to true when they are given as arguments. The predicate will only be evaluated until such pair is found or until all the keys and values of the map have been used as arguments. That is, there may be keys and values of the map that are never used as arguments to the predicate. The result is null if predicate does not evaluate to true for any key/value combination.
         Pair<City, Person> detected =
             peopleByCity.detect((City city, Person person) -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
         
        Specified by:
        detect in interface MapIterable<K,​V>
        Overrides:
        detect in class AbstractMutableMapIterable<K,​V>
      • detect

        public V detect​(Predicate<? super V> predicate)
        Description copied from interface: RichIterable
        Returns the first element of the iterable for which the predicate evaluates to true or null in the case where no element returns true. This method is commonly called find.

        Example using a Java 8 lambda expression:

         Person person =
             people.detect(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
         

        Example using an anonymous inner class:

         Person person =
             people.detect(new Predicate<Person>()
             {
                 public boolean accept(Person person)
                 {
                     return person.getFirstName().equals("John") && person.getLastName().equals("Smith");
                 }
             });
         
        Specified by:
        detect in interface RichIterable<K>
        Overrides:
        detect in class AbstractMapIterable<K,​V>
      • detectWith

        public <P> V detectWith​(Predicate2<? super V,​? super P> predicate,
                                P parameter)
        Description copied from interface: RichIterable
        Returns the first element that evaluates to true for the specified predicate2 and parameter, or null if none evaluate to true.

        Example using a Java 8 lambda expression:

         Person person =
             people.detectWith((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
         

        Example using an anonymous inner class:

         Person person =
             people.detectWith(new Predicate2<Person, String>()
             {
                 public boolean accept(Person person, String fullName)
                 {
                     return person.getFullName().equals(fullName);
                 }
             }, "John Smith");
         
        Specified by:
        detectWith in interface RichIterable<K>
        Overrides:
        detectWith in class AbstractMapIterable<K,​V>
      • detectOptional

        public java.util.Optional<Pair<K,​V>> detectOptional​(Predicate2<? super K,​? super V> predicate)
        Description copied from interface: MapIterable
        Return the first key and value of the map as an Optional for which the predicate evaluates to true when they are given as arguments. The predicate will only be evaluated until such pair is found or until all the keys and values of the map have been used as arguments. That is, there may be keys and values of the map that are never used as arguments to the predicate.
         Optional<Pair<City, Person>> detected =
             peopleByCity.detectOptional((city, person)
                  -> city.getName().equals("Anytown") && person.getLastName().equals("Smith"));
         
        Specified by:
        detectOptional in interface MapIterable<K,​V>
        Overrides:
        detectOptional in class AbstractMutableMapIterable<K,​V>
      • detectOptional

        public java.util.Optional<V> detectOptional​(Predicate<? super V> predicate)
        Description copied from interface: RichIterable
        Returns the first element of the iterable for which the predicate evaluates to true as an Optional. This method is commonly called find.

        Example using a Java 8 lambda expression:

         Person person =
             people.detectOptional(person -> person.getFirstName().equals("John") && person.getLastName().equals("Smith"));
         

        Specified by:
        detectOptional in interface RichIterable<K>
        Overrides:
        detectOptional in class AbstractMapIterable<K,​V>
      • detectWithOptional

        public <P> java.util.Optional<V> detectWithOptional​(Predicate2<? super V,​? super P> predicate,
                                                            P parameter)
        Description copied from interface: RichIterable
        Returns the first element that evaluates to true for the specified predicate2 and parameter as an Optional.

        Example using a Java 8 lambda expression:

         Optional<Person> person =
             people.detectWithOptional((person, fullName) -> person.getFullName().equals(fullName), "John Smith");
         

        Specified by:
        detectWithOptional in interface RichIterable<K>
        Overrides:
        detectWithOptional in class AbstractMapIterable<K,​V>
      • detectIfNone

        public V detectIfNone​(Predicate<? super V> predicate,
                              Function0<? extends V> function)
        Description copied from interface: RichIterable
        Returns the first element of the iterable for which the predicate evaluates to true. If no element matches the predicate, then returns the value of applying the specified function.
        Specified by:
        detectIfNone in interface RichIterable<K>
        Overrides:
        detectIfNone in class AbstractMapIterable<K,​V>
      • shortCircuit

        private boolean shortCircuit​(Predicate<? super V> predicate,
                                     boolean expected,
                                     boolean onShortCircuit,
                                     boolean atEnd)
      • shortCircuitWith

        private <P> boolean shortCircuitWith​(Predicate2<? super V,​? super P> predicate,
                                             P parameter,
                                             boolean expected,
                                             boolean onShortCircuit,
                                             boolean atEnd)
      • anySatisfy

        public boolean anySatisfy​(Predicate<? super V> predicate)
        Description copied from interface: RichIterable
        Returns true if the predicate evaluates to true for any element of the iterable. Returns false if the iterable is empty, or if no element returned true when evaluating the predicate.
        Specified by:
        anySatisfy in interface RichIterable<K>
        Overrides:
        anySatisfy in class AbstractMapIterable<K,​V>
      • anySatisfyWith

        public <P> boolean anySatisfyWith​(Predicate2<? super V,​? super P> predicate,
                                          P parameter)
        Description copied from interface: RichIterable
        Returns true if the predicate evaluates to true for any element of the collection, or return false. Returns false if the collection is empty.
        Specified by:
        anySatisfyWith in interface RichIterable<K>
        Overrides:
        anySatisfyWith in class AbstractMapIterable<K,​V>
      • noneSatisfyWith

        public <P> boolean noneSatisfyWith​(Predicate2<? super V,​? super P> predicate,
                                           P parameter)
        Description copied from interface: RichIterable
        Returns true if the predicate evaluates to false for every element of the collection, or return false. Returns true if the collection is empty.
        Specified by:
        noneSatisfyWith in interface RichIterable<K>
        Overrides:
        noneSatisfyWith in class AbstractMapIterable<K,​V>
      • nullSafeEquals

        private static boolean nullSafeEquals​(java.lang.Object value,
                                              java.lang.Object other)
      • nonSentinel

        private K nonSentinel​(java.lang.Object key)
      • toSentinelIfNull

        private static java.lang.Object toSentinelIfNull​(java.lang.Object key)
      • nonNullTableObjectEquals

        private boolean nonNullTableObjectEquals​(java.lang.Object cur,
                                                 K key)