Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

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

  • December 26, 2023
  • 6214 Unique Views
  • 3 min read
Table of Contents
Understanding the Need for JEP 431Introducing Sequenced Collections with JEP 431Sequenced MapsRetrofitting and CompatibilityAddressing Risks and Forward CompatibilityConclusion

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

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!

9 Outdated Ideas About Java

In this article, we want to look into some false assumptions and outdated ideas about Java based on early versions.

Deserialization Exploits in Java: Why Should I Care?

Deserialization vulnerabilities work natively in Java and how attack chains are created. This problem is also not restricted to Java’s custom serialization framework. When deserializing JSON, XML, or YAML, similar issues can occur as well. This talk shows some pointers on how to mitigate these problems in your own applications.

Do You Trust Profilers? I Once Did Too.

Do you trust profilers? I once did too. Here I describe why you should take the results of profilers with a grain of salt.

Busting Myths, Building Futures: A Conversation with Cay Horstmann on Java and Machine Learning

Cay Horstmann shares his experiences with Java, his writing process for technical books, the challenges of teaching Java, and discusses its role in education.

Breaking the Code: How Chris Newland is Changing the Game in JVM Performance!

In this enlightening interview, JVM performance specialist Chris Newland shares his journey in the Java universe.

Do you want your ad here?

Contact us to get your ad seen by thousands of users every day!

[email protected]

Comments (0)

Highlight your code snippets using [code lang="language name"] shortcode. Just insert your code between opening and closing tag: [code lang="java"] code [/code]. Or specify another language.

No comments yet. Be the first.

Subscribe to foojay updates:

https://foojay.io/feed/
Copied to the clipboard