Interface ObjObjMap<K,​V>

    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      void clear()
      Removes all of the mappings from this map.
      @Nullable V compute​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)
      Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).
      @Nullable V computeIfAbsent​(K key, Function<? super K,​? extends V> mappingFunction)
      If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null.
      @Nullable V computeIfPresent​(K key, BiFunction<? super K,​? super V,​? extends V> remappingFunction)
      If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
      boolean containsEntry​(Object key, Object value)
      Returns true if this map contains a mapping of the given key and value.
      boolean containsKey​(Object key)
      Returns true if this map contains a mapping for the specified key.
      boolean containsValue​(Object value)
      Returns true if this map has one or more keys associated with the specified value.
      ObjSet<Map.Entry<K,​V>> entrySet()
      Returns a Set view of the mappings contained in this map.
      boolean equals​(Object o)
      Returns true if the other object is a Map; this map and the given map have the same size; and for all entries in the other map, containsEntry(e.getKey(), e.getValue()) called on this map returns true.
      void forEach​(BiConsumer<? super K,​? super V> action)
      Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
      boolean forEachWhile​(BiPredicate<? super K,​? super V> predicate)
      Checks the given predicate on each entry in this map until all entries have been processed or the predicate returns false for some entry, or throws an Exception.
      @Nullable V get​(Object key)
      Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
      @Nullable K getInternalKey​(K key)
      Returns the key object held by this map internally and equivalent to the specified key, if there is one, or null if this map contains no mapping for the key.
      V getOrDefault​(Object key, V defaultValue)
      Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
      int hashCode()
      Returns a sum of the following expressions: keyEquivalence().hash(key) ^ valueEquivalence().hash(value) applied to all entries in this map.
      Equivalence<K> keyEquivalence()
      Returns the equivalence strategy for keys for this map.
      ObjSet<K> keySet()
      Returns a Set view of the keys contained in this map.
      default long mappingCount()
      This method is an alias to sizeAsLong().
      @Nullable V merge​(K key, V value, BiFunction<? super V,​? super V,​? extends V> remappingFunction)
      If the specified key is not already associated with a value or is associated with null, associates it with the given value.
      @Nullable V put​(K key, V value)
      Associates the specified value with the specified key in this map.
      void putAll​(Map<? extends K,​? extends V> m)
      Copies all of the mappings from the specified map to this map.
      @Nullable V putIfAbsent​(K key, V value)
      If the specified key is not already associated with a value, associates it with the given value and returns null, else returns the current value.
      @Nullable V remove​(Object key)
      Removes the mapping for a key from this map if it is present.
      boolean remove​(Object key, Object value)
      Removes the entry for the specified key only if it is currently mapped to the specified value.
      boolean removeIf​(BiPredicate<? super K,​? super V> filter)
      Removes all of the entries of this map that satisfy the given predicate.
      @Nullable V replace​(K key, V value)
      Replaces the entry for the specified key only if it is currently mapped to some value.
      boolean replace​(K key, V oldValue, V newValue)
      Replaces the entry for the specified key only if currently mapped to the specified value.
      void replaceAll​(BiFunction<? super K,​? super V,​? extends V> function)
      Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
      long sizeAsLong()
      Returns the number of entries in the map as a long value (not truncated to Integer.MAX_VALUE, if the map size exceeds it, as returned by the Map.size() method).
      Equivalence<V> valueEquivalence()
      Returns the equivalence strategy for values for this map.
      Collection<V> values()
      Returns a Collection view of the values contained in this map.
    • Method Detail

      • sizeAsLong

        long sizeAsLong()
        Returns the number of entries in the map as a long value (not truncated to Integer.MAX_VALUE, if the map size exceeds it, as returned by the Map.size() method).
        Returns:
        the number of key-value mappings in this map
        See Also:
        Map.size(), mappingCount()
      • containsKey

        boolean containsKey​(Object key)
        Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that the specified key and k are equivalent. (There can be at most one such mapping.)
        Specified by:
        containsKey in interface Map<K,​V>
        Parameters:
        key - key whose presence in this map is to be tested
        Returns:
        true if this map contains a mapping for the specified key
        Throws:
        NullPointerException - if the given key is null
      • containsEntry

        boolean containsEntry​(Object key,
                              Object value)
        Returns true if this map contains a mapping of the given key and value. More formally, this map should contain a mapping from a key k to a value v such that the specified key and k are equivalent with regard to keyEquivalence(), and the specified value and v are equivalent with regard to valueEquivalence(). (There can be at most one such mapping.)
        Parameters:
        key - the key of the mapping to check presence of
        value - the value of the mapping to check presence of
        Returns:
        true if this map contains the specified mapping, false otherwise
        Throws:
        NullPointerException - if the given key or value is null
      • getInternalKey

        @Nullable K getInternalKey​(K key)
        Returns the key object held by this map internally and equivalent to the specified key, if there is one, or null if this map contains no mapping for the key.

        This method could be used to deduplicate objects in the application, to reduce the memory footprint and make the application to conform to the "most objects die young" hypothesis that most GC algorithms are optimized for. This method is functionally similar to String.intern() and Guava's Interner, but allows to piggy-back a map data structure which may already exist in an application.

        keySet().getInternal(key) delegates to this method.

        Parameters:
        key - the key whose equivalent held by this map internally is to be returned
        Returns:
        the map-internal equivalent of the specified key, or null if the map contains no mapping for the specified key
      • get

        @Nullable V get​(Object key)
        Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

        More formally, if this map contains a mapping from a key k to a value v such that the specified key and k are equivalent, then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

        Specified by:
        get in interface Map<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value to which the specified key is mapped, or null if this map contains no mapping for the key
        Throws:
        NullPointerException - if the given key is null
      • getOrDefault

        V getOrDefault​(Object key,
                       V defaultValue)
        Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
        Specified by:
        getOrDefault in interface Map<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        defaultValue - the default mapping of the key
        Returns:
        the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
        Throws:
        NullPointerException - if the given key is null
      • put

        @Nullable V put​(K key,
                        V value)
        Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)
        Specified by:
        put in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        the previous value associated with key, or null if there was no mapping for key
        Throws:
        NullPointerException - if the given key or value is null
      • putIfAbsent

        @Nullable V putIfAbsent​(K key,
                                V value)
        If the specified key is not already associated with a value, associates it with the given value and returns null, else returns the current value.
        Specified by:
        putIfAbsent in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        value - value to be associated with the specified key
        Returns:
        the previous value associated with the specified key, or null if there was no mapping for the key
        Throws:
        NullPointerException - if the given key or value is null
      • replace

        @Nullable V replace​(K key,
                            V value)
        Replaces the entry for the specified key only if it is currently mapped to some value.
        Specified by:
        replace in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is associated
        value - value to be associated with the specified key
        Returns:
        the previous value associated with the specified key, or null if there was no mapping for the key.
        Throws:
        NullPointerException - if the given key or value is null
      • replace

        boolean replace​(K key,
                        V oldValue,
                        V newValue)
        Replaces the entry for the specified key only if currently mapped to the specified value. Values are compared using valueEquivalence().
        Specified by:
        replace in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is associated
        oldValue - value expected to be associated with the specified key
        newValue - value to be associated with the specified key
        Returns:
        true if the value was replaced
        Throws:
        NullPointerException - if the given key, oldValue or newValue is null
      • computeIfPresent

        @Nullable V computeIfPresent​(K key,
                                     BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.

        If the function returns null, the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.

        Specified by:
        computeIfPresent in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        remappingFunction - the function to compute a value
        Returns:
        the new value associated with the specified key, or null if none
        Throws:
        NullPointerException - if the given key or remappingFunction is null
      • remove

        @Nullable V remove​(Object key)
        Removes the mapping for a key from this map if it is present. More formally, if this map contains a mapping from key k to value v such that the specified key and k are equivalent, that mapping is removed. (The map can contain at most one such mapping.)

        Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.

        The map will not contain a mapping for the specified key once the call returns.

        Specified by:
        remove in interface Map<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the map
        Returns:
        the previous value associated with key, or null if there was no mapping for key
        Throws:
        NullPointerException - if the given key is null
      • remove

        boolean remove​(Object key,
                       Object value)
        Removes the entry for the specified key only if it is currently mapped to the specified value. Values are compared using valueEquivalence().
        Specified by:
        remove in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is associated
        value - value expected to be associated with the specified key
        Returns:
        true if the value was removed
        Throws:
        NullPointerException - if the given key or value is null
      • computeIfAbsent

        @Nullable V computeIfAbsent​(K key,
                                    Function<? super K,​? extends V> mappingFunction)
        If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null.

        If the function returns null no mapping is recorded. If the function itself throws an (unchecked) exception, the exception is rethrown, and no mapping is recorded. The most common usage is to construct a new object serving as an initial mapped value or memoized result, as in:

         
         map.computeIfAbsent(key, k -> new Value(f(k)));
         

        Or to implement a multi-value map, Map<K,Collection<V>>, supporting multiple values per key:

         
         map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);
         
        Specified by:
        computeIfAbsent in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        mappingFunction - the function to compute a value
        Returns:
        the current (existing or computed) value associated with the specified key, or null if the computed value is null
        Throws:
        NullPointerException - if the given key or mappingFunction is null
      • compute

        @Nullable V compute​(K key,
                            BiFunction<? super K,​? super V,​? extends V> remappingFunction)
        Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping). For example, to either create or append a String msg to a value mapping:
         
         map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))
        (Method merge() is often simpler to use for such purposes.)

        If the function returns null, the mapping is removed (or remains absent if initially absent). If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.

        Specified by:
        compute in interface Map<K,​V>
        Parameters:
        key - key with which the specified value is to be associated
        remappingFunction - the function to compute a value
        Returns:
        the new value associated with the specified key, or null if none
        Throws:
        NullPointerException - if the given key or remappingFunction is null
      • merge

        @Nullable V merge​(K key,
                          V value,
                          BiFunction<? super V,​? super V,​? extends V> remappingFunction)
        If the specified key is not already associated with a value or is associated with null, associates it with the given value. Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null. This method may be of use when combining multiple mapped values for a key. For example, to either create or append a String msg to a value mapping:
         
         map.merge(key, msg, String::concat)
         

        If the function returns null the mapping is removed. If the function itself throws an (unchecked) exception, the exception is rethrown, and the current mapping is left unchanged.

        Specified by:
        merge in interface Map<K,​V>
        Parameters:
        key - key with which the resulting value is to be associated
        value - the value to be merged with the existing value associated with the key or, if no existing value is associated with the key, to be associated with the key
        remappingFunction - the function to recompute a value if present
        Returns:
        the new value associated with the specified key, or null if no value is associated with the key
        Throws:
        NullPointerException - if the given key, value or remappingFunction is null
      • forEach

        void forEach​(BiConsumer<? super K,​? super V> action)
        Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. Actions are performed in the order of entry set iteration. Exceptions thrown by the action are relayed to the caller.

        The entries will be processed in the same order as they appear the entry set's iterator and forEachWhile(BiPredicate).

        Specified by:
        forEach in interface Map<K,​V>
        Parameters:
        action - The action to be performed for each entry
        Throws:
        NullPointerException - if the specified action is null
        ConcurrentModificationException - if any structural modification of the map (new entry insertion or an entry removal) is detected during iteration
        See Also:
        forEachWhile(BiPredicate)
      • forEachWhile

        boolean forEachWhile​(BiPredicate<? super K,​? super V> predicate)
        Checks the given predicate on each entry in this map until all entries have been processed or the predicate returns false for some entry, or throws an Exception. Exceptions thrown by the predicate are relayed to the caller.

        The entries will be processed in the same order as they appear in the entry set's iterator and forEach(BiConsumer).

        If the map is empty, this method returns true immediately.

        Parameters:
        predicate - the predicate to be checked for each entry
        Returns:
        true if the map is empty, or if the predicate returned true for all entries of the map, false if the predicate returned false for some entry
        Throws:
        NullPointerException - if the specified predicate is null
        ConcurrentModificationException - if any structural modification of the map (new entry insertion or an entry removal) is detected during iteration
        See Also:
        forEach(BiConsumer)
      • replaceAll

        void replaceAll​(BiFunction<? super K,​? super V,​? extends V> function)
        Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are relayed to the caller.
        Specified by:
        replaceAll in interface Map<K,​V>
        Parameters:
        function - the function to apply to each entry
        Throws:
        NullPointerException - if the specified function is null
        ConcurrentModificationException - if any structural modification of the map (new entry insertion or an entry removal) is detected during iteration
      • containsValue

        boolean containsValue​(Object value)
        Returns true if this map has one or more keys associated with the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that valueEquivalence().equivalent(value, v) == true. This operation requires time linear in the map size.
        Specified by:
        containsValue in interface Map<K,​V>
        Parameters:
        value - value whose presence in this map is to be tested
        Returns:
        true if this map maps one or more keys to the specified value
      • putAll

        void putAll​(Map<? extends K,​? extends V> m)
        Copies all of the mappings from the specified map to this map. The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
        Specified by:
        putAll in interface Map<K,​V>
        Parameters:
        m - mappings to be stored in this map
        Throws:
        NullPointerException - if the specified map is null
      • clear

        void clear()
        Removes all of the mappings from this map. The map will be empty after this call returns.
        Specified by:
        clear in interface Map<K,​V>
        Throws:
        ConcurrentModificationException - if any structural modification of the map (new entry insertion or an entry removal) is detected during operation
      • removeIf

        boolean removeIf​(BiPredicate<? super K,​? super V> filter)
        Removes all of the entries of this map that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.

        Note the order in which this method visits entries may be different from the iteration and forEach(BiConsumer) order.

        Parameters:
        filter - a predicate which returns true for entries to be removed
        Returns:
        true if any entries were removed
        Throws:
        NullPointerException - if the specified filter is null
        ConcurrentModificationException - if any structural modification of the map (new entry insertion or an entry removal) is detected during iteration
      • hashCode

        int hashCode()
        Returns a sum of the following expressions: keyEquivalence().hash(key) ^ valueEquivalence().hash(value) applied to all entries in this map. Note that if this map has a custom keyEquivalence() or valueEquivalence(), the resulting hash code may be inconsistent with the general Map.hashCode() contract.
        Specified by:
        hashCode in interface Map<K,​V>
        Overrides:
        hashCode in class Object