5 minutes
Devoxx BE 2025: It Only Starts with a Container & How Abstraction Becomes Reality
At Devoxx Belgium 2025, I was able to talk about what happens after you build your container. In theory, […]
Java has been a staple in the software development world for decades, renowned for its robustness and vast ecosystem. However, some seasoned Java developers have encountered limitations within its collections framework, particularly when dealing with ordered elements. Enter JEP 431. JEP 431, part of the Java Enhancement Proposal system, represents a significant advancement in Java’s ongoing evolution. The Java Enhancement Proposal system is a process for proposing, reviewing, and implementing new features in the Java programming language. JEP 431, in particular, introduces necessary enhancements to the Java Collections Framework, addressing longstanding limitations and expanding its capabilities for us Java developers.
Historically, Java’s collections framework, while comprehensive, lacked a specific collection type that could represent a sequence of elements with a defined order. This gap was more than a minor inconvenience for some larger projects. Take, for instance, the List and Deque interfaces – both define an encounter order, but their common supertype, Collection, does not. Similarly, while Set does not define an encounter order, some subtypes like SortedSet and LinkedHashSet do.
This inconsistent support across the hierarchy made it difficult to express and handle ordered collections uniformly. You’d often face challenges in iterating collections in reverse order or implementing specific operations for ordered collections, leading to inefficient and cumbersome solutions.
JEP 431 introduces new interfaces: sequenced collections, sequenced sets, and sequenced maps. These interfaces have been integrated into the existing collections framework, bringing uniformity and enhanced functionality.
A sequenced collection is essentially a Collection with a defined encounter order. Each element in this collection has a well-defined position – first, second, and so on, up to the last element. The key features include:
A sequenced set is a Set that also behaves as a sequenced collection. This means no duplicate elements, but with the added ability to maintain a specific sequence. Notably, methods like addFirst(E) and addLast(E) can reposition elements if they are already present in the set, addressing a long-standing limitation in LinkedHashSet.
SequencedSet<String> sequencedSet = new LinkedHashSet<>();
sequencedSet.addFirst("Apple");
sequencedSet.add("Banana");
sequencedSet.addLast("Cherry");
SequencedSet<String> sequencedSet = new LinkedHashSet<>();
sequencedSet.addFirst("Apple");
sequencedSet.add("Banana");
sequencedSet.addLast("Cherry");
assertEquals("Apple", sequencedSet.getFirst());
assertEquals("Cherry", sequencedSet.getLast());
SequencedCollection<String> sequencedCollection = new ArrayList<>();
sequencedCollection.addFirst("Apple");
sequencedCollection.add("Banana");
sequencedCollection.addLast("Cherry");
assertEquals("Apple", sequencedCollection.getFirst());
assertEquals("Cherry", sequencedCollection.getLast());
Sequenced maps represent Map entries with a defined order. This interface introduces methods to get and manipulate entries in a specific sequence, including putting entries at the start or end of the map. It also introduces methods for getting a sequenced collection of the keys and values of the map with the sequencedKeySet and sequencedValues. There is also a method for getting a sequenced collection of the entry set of the map.
SequencedMap<String, Integer> sequencedMap = new LinkedHashMap<>();
sequencedMap.putFirst("Apple", 10);
sequencedMap.put("Banana", 20);
sequencedMap.putLast("Cherry", 30);
assertEquals("Apple", sequencedMap.firstEntry().getKey());
assertEquals(10, sequencedMap.firstEntry().getValue());
assertEquals("Cherry", sequencedMap.lastEntry().getKey());
assertEquals(30, sequencedMap.lastEntry().getValue());
The retrofitting of these interfaces into existing classes and interfaces like List, Deque, LinkedHashSet, SortedSet, LinkedHashMap, and SortedMap ensures backward compatibility while expanding functionality, an important aspect in a widely used language like Java.
With any significant addition to a language’s core features, there are risks and concerns, especially regarding backward compatibility. JEP 43 introduces methods that are compatible with existing interfaces and carefully considers the impact of new methods high in the inheritance hierarchy.
JEP 431 marks a significant milestone in Java’s evolution. By addressing a long-standing gap in the collections framework, it not only enhances the language’s capabilities but also simplifies our development experience with the language. Java developers can now handle ordered collections more efficiently, paving the way for more streamlined and effective code. With this update, Java reaffirms its commitment to evolving in response to its community’s needs, ensuring it remains a top choice for developers worldwide. Happy Coding!
Share:
5 minutes
At Devoxx Belgium 2025, I was able to talk about what happens after you build your container. In theory, […]
5 minutes
Welcome aboard the October issue of The Monthly Catch!As the leaves turn and conference season hits full stride, the […]
2 minutes
How outdated Java systems are draining budgets and throttling innovation across financial services? Let’s dig in in this blog […]
What impact will this have on Payara? Are there any plans to modify Payara in a way that makes explicit use of this?