[JEP 431] Java JDK 21 New Feature - Sequenced Collections


Hello Java Developer!

Welcome to the Java JDK 21 Series where we cover all the new features that are introduced in this latest LTS Version.


Introduction to JEP 431

    The JEP 431 is known as Sequenced Collections, It introduces some new interfaces and methods to represent collections in Java that maintain a specific order for their elements. This JEP addresses the lack of a standardized way to work with ordered collections in Java's existing collections framework, which has led to inconsistencies and limitations in working with such collections.

    The main motivations are to resolve the below questions related to ordered collections in Java,

    1. Variability in ordered elements support.
    2. Lack of consistency in supertype.
    3. Absence of standardized methods.
    4. Complexity in Reverse Iteration.

What's are Sequenced Collections

    These are three new interfaces that are added to the Java Collections framework SequencedCollection, SequencedSet and SequencedMap.

    JDK 21 - Sequenced Collections​ - JEP 431

    As you may see prior to Java JDK 21, we can fetch the first and the last element of an ArrayList as follows,

    List<String> unorderedCollection = new ArrayList<>();
    
    unorderedCollection.add("Sam");
    unorderedCollection.add("Alex");
    unorderedCollection.add("Jim");
    
    String listFirstElement = unorderedCollection.iterator().next(); 
    String listLastElement = unorderedCollection.get(unorderedCollection.size() - 1);

    Now with the new SequencedCollection interface getFirst() and getLast() methods, this looks much simpler and easier to read.

    SequencedCollection.java (java.util.SequencedCollection)
    
    
    default E getFirst()
    default E getLast()
    String listFirstItem = unorderedCollection.getFirst();
    String listLastItem = unorderedCollection.getLast();

    Do take a look at the other methods introduced in SequencedCollection.java which are self explanatory.

    default E getFirst()
    default E getLast()
    
    default E removeFirst()
    default E removeLast()
    
    SequencedCollection<E> reversed()

    Similarly lets take a look at SequencedSet. The Sequenced Set is a Set that is also a Sequenced Collection with no duplicate elements, It inherits the methods from the Sequenced Collection and includes specialized methods for adding elements while maintaining order.

    SequencedSet.java  (java.util.SequencedSet)
    
    SequencedSet<E> reversed()

    Example:

        public static void main(String... args) {
    
            LinkedHashSet<String> linkedHashSet = new LinkedHashSet();
            linkedHashSet.add("Sam");
            linkedHashSet.add("Alex");
            linkedHashSet.add("Mike");
    
            linkedHashSet.addFirst("Firsty");
            linkedHashSet.addLast("Lasty");
    
            System.out.println(linkedHashSet.reversed());
    
        }

    Output:

    [Lasty, Mike, Alex, Sam, Firsty]


    Finally, let's take a look at SequencedMap.

    SequencedMap.java (java.util.SequencedMap)
    
    default Map.Entry firstEntry()
    default Map.Entry lastEntry()
    default Map.Entry pollFirstEntry()
    default Map.Entry pollLastEntry()
    default V putFirst(K k, V v)
    default V putLast(K k, V v)
    
    SequencedMap<K,V> reversed()
    
    default SequencedSet<Map.Entry> sequencedEntrySet()
    default SequencedSet<K> sequencedKeySet()
    default SequencedCollection<V> sequencedValues()
    public static void main(String... args) {
    
            LinkedHashMap<String, String> countryMap = new LinkedHashMap<>();
            countryMap.put("USA", "Chicago");
            countryMap.put("India", "New Delhi");
            countryMap.put("Japan", "Tokyo");
    
            countryMap.putFirst("France", "Paris");
            countryMap.putLast("UK", "London");
    
            System.out.println(countryMap.firstEntry()); 
            System.out.println(countryMap.lastEntry()); 
    
            System.out.println(countryMap.pollFirstEntry()); 
            System.out.println(countryMap.pollLastEntry()); 
        
            System.out.println(countryMap.reversed()); 
    
        }

Conclusion

tl;dr - Sequenced Collections

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap