Interface SequencedMap<K,V>

Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
All Superinterfaces:
Map<K,V>
All Known Subinterfaces:
ConcurrentNavigableMap<K,V>,NavigableMap<K,V>,SortedMap<K,V>
All Known Implementing Classes:
ConcurrentSkipListMap,LinkedHashMap,TreeMap

public interfaceSequencedMap<K,V>extendsMap<K,V>
A Map that has a well-defined encounter order, that supports operations at both ends, and that is reversible. Theencounter order of aSequencedMap is similar to that of the elements of aSequencedCollection, but the ordering applies to mappings instead of individual elements.

The bulk operations on this map, including theforEach and thereplaceAll methods, operate on this map's mappings in encounter order.

The view collections provided by thekeySet,values,entrySet,sequencedKeySet,sequencedValues, andsequencedEntrySet methods all reflect the encounter order of this map. Even though the return values of thekeySet,values, andentrySet methods are not sequencedtypes, the elements in those view collections do reflect the encounter order of this map. Thus, the iterators returned by the statements

    var it1 = sequencedMap.entrySet().iterator();    var it2 = sequencedMap.sequencedEntrySet().iterator();
both provide the mappings ofsequencedMap in that map's encounter order.

This interface provides methods to add mappings, to retrieve mappings, and to remove mappings at either end of the map's encounter order.

This interface also defines thereversed() method, which provides a reverse-orderedview of this map. In the reverse-ordered view, the concepts of first and last are inverted, as are the concepts of successor and predecessor. The first mapping of this map is the last mapping of the reverse-ordered view, and vice-versa. The successor of some mapping in this map is its predecessor in the reversed view, and vice-versa. All methods that respect the encounter order of the map operate as if the encounter order is inverted. For instance, theforEach method of the reversed view reports the mappings in order from the last mapping of this map to the first. In addition, all of the view collections of the reversed view also reflect the inverse of this map's encounter order. For example,

    var itr = sequencedMap.reversed().entrySet().iterator();
provides the mappings of this map in the inverse of the encounter order, that is, from the last mapping to the first mapping. The availability of thereversed method, and its impact on the ordering semantics of all applicable methods and views, allow convenient iteration, searching, copying, and streaming of this map's mappings in either forward order or reverse order.

A map's reverse-ordered view is generally not serializable, even if the original map is serializable.

TheMap.Entry instances obtained by iterating theMap.entrySet() view, thesequencedEntrySet() view, and its reverse-ordered view, maintain a connection to the underlying map. This connection is guaranteed only during the iteration. It is unspecified whether the connection is maintained outside of the iteration. If the underlying map permits it, calling an Entry'ssetValue method will modify the value of the underlying mapping. It is, however, unspecified whether modifications to the value in the underlying mapping are visible in theEntry instance.

The methodsfirstEntry(),lastEntry(),pollFirstEntry(), andpollLastEntry() returnMap.Entry instances that represent snapshots of mappings as of the time of the call. They donot support mutation of the underlying map via the optionalsetValue method.

Depending upon the implementation, theEntry instances returned by other means might or might not be connected to the underlying map. For example, consider anEntry obtained in the following manner:

    var entry = sequencedMap.sequencedEntrySet().getFirst();
It is not specified by this interface whether thesetValue method of theEntry thus obtained will update a mapping in the underlying map, or whether it will throw an exception, or whether changes to the underlying map are visible in thatEntry.

This interface has the same requirements on theequals andhashCode methods as defined byMap.equals andMap.hashCode. Thus, aMap and aSequencedMap will compare equals if and only if they have equal mappings, irrespective of ordering.

This class is a member of the Java Collections Framework.

Since:
21
  • Method Details

    • reversed

      SequencedMap<K,V> reversed()
      Returns a reverse-orderedview of this map. The encounter order of mappings in the returned view is the inverse of the encounter order of mappings in this map. The reverse ordering affects all order-sensitive operations, including those on the view collections of the returned view. If the implementation permits modifications to this view, the modifications "write through" to the underlying map. Changes to the underlying map might or might not be visible in this reversed view, depending upon the implementation.
      Returns:
      a reverse-ordered view of this map
    • firstEntry

      default Map.Entry<K,V> firstEntry()
      Returns the first key-value mapping in this map, ornull if the map is empty.
      Implementation Requirements:
      The implementation in this interface obtains the iterator of this map's entrySet. If the iterator has an element, it returns an unmodifiable copy of that element. Otherwise, it returns null.
      Returns:
      the first key-value mapping, ornull if this map is empty
    • lastEntry

      default Map.Entry<K,V> lastEntry()
      Returns the last key-value mapping in this map, ornull if the map is empty.
      Implementation Requirements:
      The implementation in this interface obtains the iterator of the entrySet of this map's reversed view. If the iterator has an element, it returns an unmodifiable copy of that element. Otherwise, it returns null.
      Returns:
      the last key-value mapping, ornull if this map is empty
    • pollFirstEntry

      default Map.Entry<K,V> pollFirstEntry()
      Removes and returns the first key-value mapping in this map, ornull if the map is empty (optional operation).
      Implementation Requirements:
      The implementation in this interface obtains the iterator of this map's entrySet. If the iterator has an element, it callsremove on the iterator and then returns an unmodifiable copy of that element. Otherwise, it returns null.
      Returns:
      the removed first entry of this map, ornull if this map is empty
      Throws:
      UnsupportedOperationException - if this collection implementation does not support this operation
    • pollLastEntry

      default Map.Entry<K,V> pollLastEntry()
      Removes and returns the last key-value mapping in this map, ornull if the map is empty (optional operation).
      Implementation Requirements:
      The implementation in this interface obtains the iterator of the entrySet of this map's reversed view. If the iterator has an element, it callsremove on the iterator and then returns an unmodifiable copy of that element. Otherwise, it returns null.
      Returns:
      the removed last entry of this map, ornull if this map is empty
      Throws:
      UnsupportedOperationException - if this collection implementation does not support this operation
    • putFirst

      default V putFirst(K k,V v)
      Inserts the given mapping into the map if it is not already present, or replaces the value of a mapping if it is already present (optional operation). After this operation completes normally, the given mapping will be present in this map, and it will be the first mapping in this map's encounter order.
      Implementation Requirements:
      The implementation in this interface always throwsUnsupportedOperationException.
      Parameters:
      k - the key
      v - the value
      Returns:
      the value previously associated with k, or null if none
      Throws:
      UnsupportedOperationException - if this collection implementation does not support this operation
    • putLast

      default V putLast(K k,V v)
      Inserts the given mapping into the map if it is not already present, or replaces the value of a mapping if it is already present (optional operation). After this operation completes normally, the given mapping will be present in this map, and it will be the last mapping in this map's encounter order.
      Implementation Requirements:
      The implementation in this interface always throwsUnsupportedOperationException.
      Parameters:
      k - the key
      v - the value
      Returns:
      the value previously associated with k, or null if none
      Throws:
      UnsupportedOperationException - if this collection implementation does not support this operation
    • sequencedKeySet

      default SequencedSet<K> sequencedKeySet()
      Returns aSequencedSet view of this map'skeySet.
      Implementation Requirements:
      The implementation in this interface returns aSequencedSet instance that behaves as follows. Itsadd andaddAll methods throwUnsupportedOperationException. Itsreversed method returns thesequencedKeySet view of thereversed view of this map. Each of its other methods calls the corresponding method of thekeySet view of this map.
      Returns:
      aSequencedSet view of this map'skeySet
    • sequencedValues

      default SequencedCollection<V> sequencedValues()
      Returns aSequencedCollection view of this map'svalues collection.
      Implementation Requirements:
      The implementation in this interface returns aSequencedCollection instance that behaves as follows. Itsadd andaddAll methods throwUnsupportedOperationException. Itsreversed method returns thesequencedValues view of thereversed view of this map. Itsequals andhashCode methods are inherited fromObject. Each of its other methods calls the corresponding method of thevalues view of this map.
      Returns:
      aSequencedCollection view of this map'svalues collection
    • sequencedEntrySet

      default SequencedSet<Map.Entry<K,V>> sequencedEntrySet()
      Returns aSequencedSet view of this map'sentrySet.
      Implementation Requirements:
      The implementation in this interface returns aSequencedSet instance that behaves as follows. Itsadd andaddAll methods throwUnsupportedOperationException. Itsreversed method returns thesequencedEntrySet view of thereversed view of this map. Each of its other methods calls the corresponding method of theentrySet view of this map.
      Returns:
      aSequencedSet view of this map'sentrySet