
Hi and welcome, Igor Fraga here!
Today I’m starting a series of posts called “The upgraded Java Developer” about the latest Java programming language enhancements for you that are old school Java like me and for you that are learning to program in Java as well, to be up to date and use these features in your daily work.
Each article from these series will bring new language resources that came to make our lives much easier, enhance performance and evolve the Java language, so Java is not the ugly duck anymore that guys from other languages liked to complain about.
It has evolved considerably these last years and with this series you will learn in a easy to understand approach and how to apply in practice everything that is presented here. We will be covering the evolution since Java 8, going through the many version changes until the latest LTS version by the time of this writting that is Java 21. So, let’s get started!
Today we’re exploring Sequenced Collections, an exciting feature introduced in Java 21. If you’re used to Java 8, this addition will significantly improve how you work with ordered data structures.
What are Sequenced Collections?
Sequenced Collections are a set of new interfaces added to the Java Collections Framework. They provide a uniform way to work with collections that have a defined encounter order, such as lists, sorted sets, and linked hash sets.
How Sequenced Collections Work
Here’s a simple example of how to use a Sequenced Collection:
SequencedCollection<String> sequenced = new ArrayList<>();
sequenced.addFirst("First");
sequenced.addLast("Last");
sequenced.add("Middle");
System.out.println(sequenced.getFirst()); // Outputs: First
System.out.println(sequenced.getLast()); // Outputs: Last
SequencedCollection<String> reversed = sequenced.reversed();
System.out.println(reversed.getFirst()); // Outputs: Last
In this example, we use new methods like addFirst()
, addLast()
, getFirst()
, and getLast()
which are now available on collections that implement the SequencedCollection
interface.
Why are Sequenced Collections Better?
Sequenced Collections offer several advantages:
-
Consistency: They provide a uniform API for working with ordered collections.
-
Simplicity: Accessing first and last elements is now straightforward across different collection types.
-
Reversibility: The
reversed()
method allows easy creation of a reverse view of the collection. -
Enhanced Functionality: New methods like
addFirst()
andaddLast()
add flexibility to collection manipulation.
New Interfaces in Java 21
Java 21 introduces three new interfaces:
-
SequencedCollection
: For collections with a defined order. -
SequencedSet
: For sets that maintain insertion order. -
SequencedMap
: For maps that maintain a specific entry order.
Conclusion
Sequenced Collections in Java 21 represent a significant improvement in how we work with ordered data structures. They simplify common operations, enhance readability, and provide a more intuitive API for developers working with collections that have a defined order.
In our next article, we’ll explore Pattern Matching for switch, another powerful feature that has evolved Java’s syntax. Stay tuned to learn how this feature can simplify your conditional logic!