Introduction
The SequencedSet
interface in Java, introduced in Java 21, is part of the java.util
package. It extends the Set
interface and provides methods to access elements in a sequence, focusing on maintaining and accessing elements in a specific order, such as insertion order.
Table of Contents
- What is the
SequencedSet
Interface? - Common Methods
- Examples of Using the
SequencedSet
Interface - Conclusion
1. What is the SequencedSet Interface?
The SequencedSet
interface extends the Set
interface and provides methods to access elements based on their sequence within the set. This interface is useful for sets that maintain a specific order of elements, such as insertion order. Implementations of this interface ensure that elements can be accessed and manipulated in a sequence-preserving manner.
2. Common Methods
reversed()
: Returns aSequencedSet
in reverse order.addFirst(E e)
: Inserts the specified element at the beginning of the set.addLast(E e)
: Appends the specified element to the end of the set.getFirst()
: Returns the first element in the set.getLast()
: Returns the last element in the set.removeFirst()
: Removes and returns the first element from the set.removeLast()
: Removes and returns the last element from the set.
3. Examples of Using the SequencedSet Interface
Example 1: Basic Usage with LinkedHashSet
This example demonstrates how to use a LinkedHashSet
with the SequencedSet
interface.
import java.util.LinkedHashSet;
import java.util.SequencedSet;
public class SequencedSetExample {
public static void main(String[] args) {
SequencedSet<String> set = new LinkedHashSet<>();
// Adding elements to the set
set.addFirst("First");
set.addLast("Second");
set.addLast("Third");
// Accessing the first and last elements
System.out.println("First Element: " + set.getFirst());
System.out.println("Last Element: " + set.getLast());
// Removing the first and last elements
System.out.println("Removed First Element: " + set.removeFirst());
System.out.println("Removed Last Element: " + set.removeLast());
// Reversed set
SequencedSet<String> reversedSet = set.reversed();
System.out.println("Reversed Set: " + reversedSet);
}
}
This example shows how to use a TreeSet
with the SequencedSet
interface.
Output:
First Element: First
Last Element: Third
Removed First Element: First
Removed Last Element: Third
Reversed Set: [Second]
Example 2: Using SequencedSet
with TreeSet
This example shows how to use a TreeSet
with the SequencedSet
interface.
import java.util.SequencedSet;
import java.util.TreeSet;
public class TreeSetSequencedSetExample {
public static void main(String[] args) {
SequencedSet<String> set = new TreeSet<>();
// Adding elements to the set
set.add("Apple");
set.add("Banana");
set.add("Cherry");
// Accessing the first and last elements
System.out.println("First Element: " + set.getFirst());
System.out.println("Last Element: " + set.getLast());
// Removing the first and last elements
System.out.println("Removed First Element: " + set.removeFirst());
System.out.println("Removed Last Element: " + set.removeLast());
// Reversed set
SequencedSet<String> reversedSet = set.reversed();
System.out.println("Reversed Set: " + reversedSet);
}
}
Output:
First Element: Apple
Last Element: Cherry
Removed First Element: Apple
Removed Last Element: Cherry
Reversed Set: [Banana]
Example 3: Using SequencedSet
with ConcurrentSkipListSet
This example demonstrates how to use a ConcurrentSkipListSet
with the SequencedSet
interface.
import java.util.SequencedSet;
import java.util.concurrent.ConcurrentSkipListSet;
public class ConcurrentSkipListSetSequencedSetExample {
public static void main(String[] args) {
SequencedSet<String> set = new ConcurrentSkipListSet<>();
// Adding elements to the set
set.add("One");
set.add("Two");
set.add("Three");
// Accessing the first and last elements
System.out.println("First Element: " + set.getFirst());
System.out.println("Last Element: " + set.getLast());
// Removing the first and last elements
System.out.println("Removed First Element: " + set.removeFirst());
System.out.println("Removed Last Element: " + set.removeLast());
// Reversed set
SequencedSet<String> reversedSet = set.reversed();
System.out.println("Reversed Set: " + reversedSet);
}
}
Output:
First Element: One
Last Element: Two
Removed First Element: One
Removed Last Element: Two
Reversed Set: [Three]
4. Conclusion
The SequencedSet
interface in Java, introduced in Java 21, provides a powerful way to manage sets that maintain a specific order of elements. By using methods to access and manipulate elements based on their sequence, developers can write more expressive and intuitive code. The examples provided demonstrate common usage patterns and highlight the capabilities of the SequencedSet
interface.
Comments
Post a Comment
Leave Comment