SequencedCollection
, to the Collections-family.SequencedCollection
, we can assume a specific order, thus we have methods that let us access, add or remove elements at both ends of the collection, without the overhead and tight coupling of more specific List
or Queue
implementations.List
implementations, especially where frequent random access is not required.Before Java 21:
Before, the Collections family hierarchy looked like this:
And, if we wanted to rely on some kind of defined order, we had to go down to a specific interface like Queue
, List
, or even some Sets like SortedSet
and LinkedHashSet
.
Even then, we are faced with annoyances like inconsistent ways to accessing the first and last element:
// Accessing the first element is inconsistent! first = list.get(0); first = deque.getFirst(); first = sortedSet.first(); first = linkedHashSet.iterator().next(); // We can't use a more general method in the super-interface first = collection.getFirst(); // ❌
After Java 17:
The new SequencedCollection
wedges itself into the existing Collections-family, meaning integrating it into existing code is seamless.
The new SequencedCollection
offers a uniform way to access, add and remove elements (at both ends of the collection).
// Accessing the first element is easy thanks to SequencedCollection first = list.getFirst(); first = deque.getFirst(); first = sortedSet.getFirst(); first = linkedHashSet.getFirst(); // We can now use a more general method in the super-interface first = sequencedCollection.getFirst(); // ✅
addFirst(E)
and addLast(E)
.removeFirst()
and removeLast()
.getFirst()
and getLast()
.reversed()
.SequencedMap
.PrimeNumsApp
-class, write a getRandomSequencedCollectionOfPrimes
-method that accepts a variable amount of Integer
arguments, and returns a SequencedCollection
with a randomly chosen implementation (e.g. ArrayList
, ArrayDeque
and LinkedHashSet
).main
-method, use your method to declare and initialise a sequenced collection, passing some prime numbers.addFirst()
, addLast()
and removeLast()
on your sequenced collection.getFirst()
, getLast()
and reversed()
methods (see screenshot).public class PrimeNumsApp { public static void main(String[] args) { SequencedCollection<Integer> primes = getRandomSequencedCollectionOfPrimes(3, 5, 7); primes.addFirst(2); primes.addLast(11); primes.addLast(13); primes.removeLast(); System.out.printf(""" %s of primes: %s First element: %s Last element: %s Reversed: %s """, primes.getClass().getSimpleName(), primes, primes.getFirst(), primes.getLast(), primes.reversed()); } private static SequencedCollection<Integer> getRandomSequencedCollectionOfPrimes(Integer... primes) { return switch (new Random().nextInt(16)) { case 0 -> new ArrayList<>(Arrays.asList(primes)); case 1 -> new ArrayDeque<>(Arrays.asList(primes)); case 2 -> new LinkedHashSet<>(Arrays.asList(primes)); default -> throw new IllegalStateException("Unknown random number"); }; } }
SequencedCollection
to the Collections framework, ensuring ordered maintenance of elements with efficient operations to manage both ends of the collection.getFirst()
, getLast()
, addFirst()
, and addLast()
for accessing and modifying elements, which eliminates the inconsistencies found in older collection types.SequencedMap
, applying ordered capabilities to map implementations with additional features for reverse viewing and managing endpoints, enhancing map usability significantly.