Class AbstractLinkedMap<K,​V>

  • Type Parameters:
    K - the type of the keys in this map
    V - the type of the values in this map
    All Implemented Interfaces:
    java.util.Map<K,​V>, Get<K,​V>, IterableGet<K,​V>, IterableMap<K,​V>, OrderedMap<K,​V>, Put<K,​V>
    Direct Known Subclasses:
    LinkedMap, LRUMap

    public abstract class AbstractLinkedMap<K,​V>
    extends AbstractHashedMap<K,​V>
    implements OrderedMap<K,​V>
    An abstract implementation of a hash-based map that links entries to create an ordered map and which provides numerous points for subclasses to override.

    This class implements all the features necessary for a subclass linked hash-based map. Key-value entries are stored in instances of the LinkEntry class which can be overridden and replaced. The iterators can similarly be replaced, without the need to replace the KeySet, EntrySet and Values view classes.

    Overridable methods are provided to change the default hashing behaviour, and to change how entries are added to and removed from the map. Hopefully, all you need for unusual subclasses is here.

    This implementation maintains order by original insertion, but subclasses may work differently. The OrderedMap interface is implemented to provide access to bidirectional iteration and extra convenience methods.

    The orderedMapIterator() method provides direct access to a bidirectional iterator. The iterators from the other views can also be cast to OrderedIterator if required.

    All the available iterators can be reset back to the start by casting to ResettableIterator and calling reset().

    The implementation is also designed to be subclassed, with lots of useful methods exposed.

    Since:
    3.0
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.util.AbstractMap

        java.util.AbstractMap.SimpleEntry<K extends java.lang.Object,​V extends java.lang.Object>, java.util.AbstractMap.SimpleImmutableEntry<K extends java.lang.Object,​V extends java.lang.Object>
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.
      boolean containsValue​(java.lang.Object value)
      Checks whether the map contains the specified value.
      K firstKey()
      Gets the first key in the map, which is the first inserted.
      K lastKey()
      Gets the last key in the map, which is the most recently inserted.
      OrderedMapIterator<K,​V> mapIterator()
      Gets an iterator over the map.
      K nextKey​(java.lang.Object key)
      Gets the next key in sequence.
      K previousKey​(java.lang.Object key)
      Gets the previous key in sequence.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, containsKey, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
      • Methods inherited from interface org.apache.commons.collections4.Put

        put, putAll
    • Method Detail

      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Checks whether the map contains the specified value.
        Specified by:
        containsValue in interface Get<K,​V>
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Overrides:
        containsValue in class AbstractHashedMap<K,​V>
        Parameters:
        value - the value to search for
        Returns:
        true if the map contains the value
        See Also:
        Map.containsValue(Object)
      • clear

        public void clear()
        Clears the map, resetting the size to zero and nullifying references to avoid garbage collection issues.
        Specified by:
        clear in interface java.util.Map<K,​V>
        Specified by:
        clear in interface Put<K,​V>
        Overrides:
        clear in class AbstractHashedMap<K,​V>
        See Also:
        Map.clear()
      • firstKey

        public K firstKey()
        Gets the first key in the map, which is the first inserted.
        Specified by:
        firstKey in interface OrderedMap<K,​V>
        Returns:
        the eldest key
      • lastKey

        public K lastKey()
        Gets the last key in the map, which is the most recently inserted.
        Specified by:
        lastKey in interface OrderedMap<K,​V>
        Returns:
        the most recently inserted key
      • nextKey

        public K nextKey​(java.lang.Object key)
        Gets the next key in sequence.
        Specified by:
        nextKey in interface OrderedMap<K,​V>
        Parameters:
        key - the key to get after
        Returns:
        the next key
      • previousKey

        public K previousKey​(java.lang.Object key)
        Gets the previous key in sequence.
        Specified by:
        previousKey in interface OrderedMap<K,​V>
        Parameters:
        key - the key to get before
        Returns:
        the previous key
      • mapIterator

        public OrderedMapIterator<K,​V> mapIterator()
        Gets an iterator over the map. Changes made to the iterator affect this map.

        A MapIterator returns the keys in the map. It also provides convenient methods to get the key and value, and set the value. It avoids the need to create an entrySet/keySet/values object. It also avoids creating the Map.Entry object.

        Specified by:
        mapIterator in interface IterableGet<K,​V>
        Specified by:
        mapIterator in interface OrderedMap<K,​V>
        Overrides:
        mapIterator in class AbstractHashedMap<K,​V>
        Returns:
        the map iterator