Revolutionising Java Collections: The Advent of Sequenced Collections in Java 21

Uncategorized

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.

Understanding the Need for JEP 431

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.

Introducing Sequenced Collections with JEP 431

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.

Sequenced Collections

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 reversed() method to provide a reverse-ordered view of the collection.
  • Methods for adding, getting, and removing elements at both ends of the collection.

Sequenced Sets

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.

Examples

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

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.

Example

  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());

Retrofitting and Compatibility

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.

Addressing Risks and Forward Compatibility

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.

Conclusion

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!

Comments (1)

Post a comment

Your email address will not be published. Required fields are marked *

Payara needs the contact information you provide to us to contact you about our products and services. You may unsubscribe from these communications at any time. For information on how to unsubscribe, as well as our privacy practices and commitment to protecting your privacy, please review our Legal & Privacy Policy.

  1. Markus Karg

    What impact will this have on Payara? Are there any plans to modify Payara in a way that makes explicit use of this?

Related Posts

How to Run and Scale AI Java Applications in Production: An Overview for Developers with no Machine Learning Expertise 9 minutes
Jakarta EE

How to Run and Scale AI Java Applications in Production: An Overview for Developers with no Machine Learning Expertise

Organizations are increasingly interested in adopting artificial intelligence (AI) and generative AI (GenAI) to improve operations and offer next-generation […]

Payara Qube-Cloud Light banner 4 minutes
Security

Zero Trust Security in Enterprise Java: What it is and How to Implement it

Cybersecurity isn’t just about building walls, fortresses, moats or any other external barrier anymore. Nowadays, it’s important to check […]

4 minutes
Uncategorized

Leading the Way: Payara Platform Community 7 Beta Now Fully Jakarta EE 11 Certified

We’re excited to announce that Payara Platform Community 7 Beta application server is now fully certified as Jakarta EE 11 […]