Introduction to JEP 431
- Variability in ordered elements support.
- Lack of consistency in supertype.
- Absence of standardized methods.
- Complexity in Reverse Iteration.
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,
What's are Sequenced Collections
These are three new interfaces that are added to the Java Collections framework SequencedCollection, SequencedSet and SequencedMap.
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.
String listFirstItem = unorderedCollection.getFirst();
String listLastItem = unorderedCollection.getLast();
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.
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.
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!
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
- Installing Java JDK 21 Final Release Candidate
- Steps to Install Java 21 (LTS JDK) on Windows 11
- How to install and Use Java JDK 21 Initial Release Candidate
- How to Enable Java JDK 21 Preview Features on IntelliJ
- Setting Up VS Code with Java JDK 21
- Java JDK 21 LTS Version Release Date (General Availability)
- [JEP 430] Java JDK 21 New Feature - String Templates (Preview)
- How to use Java JDK 21 with IntelliJ
- Java JDK 21: JEP 439 - An Improved Generational Z Garbage Collector (ZGC)
- [JEP 431] Java JDK 21 New Feature - Sequenced Collections
- Java JDK 21 - The Latest LTS Version
- Java JDK 21 - JEP 440 - Record Patterns
- Fix - Unsupported major.minor version 65.0 (Java JDK 21)
- JSON column formatting to preview SharePoint Online file on mouse hover - SharePoint
- How to list all versions of Python Modules Available using pip/pip3 - PIP
- Microsoft Teams - Where would you like to start - Business or Personal - Teams
- How to find the Length of ArrayList in Java - Java
- How to install Terraform on M1/M2 Mac - MacOS
- Fix: Error: error:0308010C:digital envelope routines::unsupported NodeJs/Vue/React - JavaScript
- Convert JSON to Gson with type as ArrayList - Java
- How to check if an element is hidden using jQuery code? - jQuery