Friday, 27 June 2025

Java 21 features in Collections

 Java 21 introduces several new features and enhancements in Collections, especially with the ongoing development of the Stream API, immutable collections, and sequenced collections.

Here are the important new features in Collections in Java 21:


✅ 1. Sequenced Collections (JEP 431) — 🔥 New!

📌 What?

Introduces a new SequencedCollection interface that adds first/last element access and reversible traversal to collections.

📦 New Interfaces:

public interface SequencedCollection<E> extends Collection<E> { E getFirst(); E getLast(); void addFirst(E e); void addLast(E e); E removeFirst(); E removeLast(); SequencedCollection<E> reversed(); }

✅ Applies to:

  • ArrayDeque

  • LinkedList

  • TreeSet

  • ArrayList (via SequencedCollection interface)

  • HashSet

  • Map types → via SequencedMap

🔍 Example:

List<String> list = new ArrayList<>(List.of("A", "B", "C")); SequencedCollection<String> seq = (SequencedCollection<String>) list; System.out.println(seq.getFirst()); // A System.out.println(seq.getLast()); // C seq.addFirst("X"); seq.addLast("Y"); System.out.println(seq); // [X, A, B, C, Y] System.out.println(seq.reversed()); // [Y, C, B, A, X]

✅ 2. Unmodifiable Collections Enhancements

Previously, Map.of(...), List.of(...), etc. returned immutable collections. With SequencedCollection, now you can traverse them in order and reverse:

SequencedCollection<Integer> nums = List.of(1, 2, 3); System.out.println(nums.reversed()); // [3, 2, 1]

✅ 3. Immutable SequencedMap and SequencedSet

You can now work with insertion-ordered maps and sets:

SequencedMap<String, Integer> map = Map.of("a", 1, "b", 2, "c", 3); System.out.println(map.firstEntry()); // a=1 System.out.println(map.lastEntry()); // c=3 System.out.println(map.reversed()); // {c=3, b=2, a=1}

✅ 4. Pattern Matching + Collections (Java 21 + Preview)

Pattern matching can now destructure collections (as a preview feature):


Object obj = List.of("hello", "world"); if (obj instanceof List<String> list && list.size() == 2) { System.out.println("First: " + list.getFirst()); }

✅ 5. Stream Improvements

Stream.mapMulti() (Added in Java 16, still crucial)

Gives better performance and flexibility than flatMap.


List<Integer> input = List.of(1, 2, 3); List<Integer> doubled = input.stream() .<Integer>mapMulti((e, consumer) -> { consumer.accept(e); consumer.accept(e * 2); }) .collect(Collectors.toList()); // Output: [1, 2, 2, 4, 3, 6]

🔮 Optional Preview Features in Java 21 (might impact future collection usage)

  • Record Patterns

  • String Templates

  • Scoped Values (for threads)


✅ Summary Table

FeatureDescriptionExample Class
SequencedCollectionNew interface for order-aware collectionsArrayList, LinkedList
reversed()Reverses any sequenced collection/mapList, Set, Map
addFirst(), getLast()First/last item accessSequencedCollection
SequencedMapOrdered maps with head/tail accessLinkedHashMap, TreeMap
Pattern Matching (preview)Destructure lists/mapsinstanceof usage

No comments:

Post a Comment