This tutorial shows you how to use SequencedSet
in Java.
Java 21 introduced an interface called SequencedCollection
, which defines a collection that has a well-defined encounter order, supports operations at both ends, and is reversible. The interface has some methods that can be useful for accessing or modifying elements. For Set
data type, there is an interface named SequencedSet
that implements the SequencedCollection
interface. Below are the usage examples.
Create SequencedSet
The LinkedHashSet
is an implementation of Set
that implements the SequencedSet
interface. Therefore, you can use a LinkedHashSet
to create a SequencedSet
. Other implementations of Set
such as HashSet
and TreeSet
do not implement the interface.
SequencedSet<String> values = new LinkedHashSet<>();
values.add("one");
values.add("two");
values.add("three");
System.out.println(values);
SequencedSet<String> emptyValues = new LinkedHashSet<>();
System.out.println(emptyValues);
Output:
[one, two, three]
[]
Get First Element
To get the first element, you can use the getFirst
method. It returns a value whose type is the Map
's value type. It will throw NoSuchElementException
if the collection is empty.
String firstElement = values.getFirst();
System.out.println(firstElement);
// String firstElementEmpty = emptyValues.getFirst(); // NoSuchElementException
Output:
one
Get Last Element
To get the last element, you can use the getLast
method. It returns a value whose type is the Map
's value type. It will throw NoSuchElementException
if the collection is empty.
String lastElement = values.getLast();
System.out.println(lastElement); // three
// String lastElementEmpty = emptyValues.getLast(); // NoSuchElementException
Output:
three
Add First Element
To add a value as the first element, you can use the addFirst
method. If the value already exists, it will be moved as the first element.
values.addFirst("zero");
System.out.println(values);
values.addFirst("one");
System.out.println(values);
Output:
[zero, one, two, three]
[one, zero, two, three]
Add Last Element
To add a value as the last element, you can use the addLast
method. If the value already exists, it will be moved as the last element.
values.addLast("four");
System.out.println(values);
values.addLast("three");
System.out.println(values);
Output:
[one, zero, two, three, four]
[one, zero, two, four, three]
Remove First Element
To remove the first element, you can use the removeFirst
method. If the Set
is empty, it will throw NoSuchElementException
values.removeFirst();
System.out.println(values);
// emptyValues.removeFirst(); // NoSuchElementException
Output:
[zero, two, four, three]
Remove Last Element
To remove the last element, you can use the removeFirst
method. If the Set
is empty, it will throw NoSuchElementException
.
values.removeLast();
System.out.println(values);
// emptyValues.removeLast(); // NoSuchElementException
Output:
[zero, two, four]
Reverse Elements
It's also possible to reverse the order of the elements by using the reversed
method. The method returns the reversed view of the collection.
SequencedSet<String> reversedSet = values.reversed();
System.out.println(values);
System.out.println(reversedSet);
Output:
[zero, two, four]
[four, two, zero]
As you can see from the output above. It doesn't modify the order of the original Set
. However, adding or removing an element on the original one affects the elements of the reversed one, and the opposite also applies. In other words, the reversed
method doesn't create a completely different sequence. Instead, it still refers to the same sequence in the reversed order.
values.addFirst("x");
System.out.println(values);
System.out.println(reversedSet);
reversedSet.addFirst("y");
System.out.println(values);
System.out.println(reversedSet);
Output:
[x, zero, two, four]
[four, two, zero, x]
[x, zero, two, four, y]
[y, four, two, zero, x]
Object Comparison
Although the elements of a SequencedSet
are ordered, the implementation of the equals
and hashCode
methods are the same as Set.equals
and Set.hashCode
. Therefore, if there are two instances of SequencedSet
whose elements are the same, they will be equal in comparison even if the elements are in different orders.
boolean isEqual = values.equals(reversedSet);
System.out.println(isEqual);
System.out.println(values.hashCode());
System.out.println(reversedSet.hashCode());
System.out.println(values.hashCode() == reversedSet.hashCode());
Output:
true
6999819
6999819
true
Full Code
package com.woolha.example.sequencedcollection;
import java.util.LinkedHashSet;
import java.util.SequencedSet;
public class SequencedSetExample {
public static void main(String[] args) {
SequencedSet<String> values = new LinkedHashSet<>();
values.add("one");
values.add("two");
values.add("three");
System.out.println(values); // [one, two, three]
SequencedSet<String> emptyValues = new LinkedHashSet<>();
System.out.println(emptyValues); // []
String firstElement = values.getFirst();
System.out.println(firstElement); // one
// String firstElementEmpty = emptyValues.getFirst(); // NoSuchElementException
String lastElement = values.getLast();
System.out.println(lastElement); // three
// String lastElementEmpty = emptyValues.getLast(); // NoSuchElementException
values.addFirst("zero");
System.out.println(values); // [zero, one, two, three]
values.addFirst("one");
System.out.println(values); // [one, zero, two, three]
values.addLast("four");
System.out.println(values); // [one, zero, two, three, four]
values.addLast("three");
System.out.println(values); // [one, zero, two, four, three]
values.removeFirst();
System.out.println(values); // [zero, two, four, three]
// emptyValues.removeFirst(); // NoSuchElementException
values.removeLast();
System.out.println(values); // [zero, two, four]
// emptyValues.removeLast(); // NoSuchElementException
SequencedSet<String> reversedSet = values.reversed();
System.out.println(values); // [zero, two, four]
System.out.println(reversedSet); // [four, two, zero]
values.addFirst("x");
System.out.println(values); // [x, zero, two, four]
System.out.println(reversedSet); // [four, two, zero, x]
reversedSet.addFirst("y");
System.out.println(values); // [x, zero, two, four, y]
System.out.println(reversedSet); // [y, four, two, zero, x]
boolean isEqual = values.equals(reversedSet);
System.out.println(isEqual); // true
System.out.println(values.hashCode()); // 6999819
System.out.println(reversedSet.hashCode()); // 6999819
System.out.println(values.hashCode() == reversedSet.hashCode()); // true
}
}
Summary
The SequencedSet
interface can be useful for a Set
with ordered elements, especially if you have to perform certain operations such as retrieving or removing an element at the first or last index. It also provides a method for reversing the elements as well. You also need to know that the comparison of two SequencedSet
objects is the same as other types of Set
which doesn't depend on the element order.
You can also read about: