Introduction
The SequencedCollection
interface in Java, introduced in Java 21, is part of the java.util
package. It extends the Collection
interface and provides methods to access elements in a sequence, specifically focusing on maintaining and accessing elements in a specific order, such as insertion order.
Table of Contents
- What is the
SequencedCollection
Interface? - Common Methods
- Examples of Using the
SequencedCollection
Interface - Conclusion
1. What is the SequencedCollection Interface?
The SequencedCollection
interface extends the Collection
interface and provides methods to access elements based on their sequence within the collection. This interface is useful for collections 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 aSequencedCollection
in reverse order.addFirst(E e)
: Inserts the specified element at the beginning of the collection.addLast(E e)
: Appends the specified element to the end of the collection.getFirst()
: Returns the first element in the collection.getLast()
: Returns the last element in the collection.removeFirst()
: Removes and returns the first element from the collection.removeLast()
: Removes and returns the last element from the collection.
3. Examples of Using the SequencedCollection Interface
Example 1: Basic Usage with ArrayDeque
This example demonstrates how to use an ArrayDeque
with the SequencedCollection
interface.
import java.util.ArrayDeque;
import java.util.SequencedCollection;
public class SequencedCollectionExample {
public static void main(String[] args) {
SequencedCollection<String> collection = new ArrayDeque<>();
// Adding elements to the collection
collection.addFirst("First");
collection.addLast("Second");
collection.addLast("Third");
// Accessing the first and last elements
System.out.println("First Element: " + collection.getFirst());
System.out.println("Last Element: " + collection.getLast());
// Removing the first and last elements
System.out.println("Removed First Element: " + collection.removeFirst());
System.out.println("Removed Last Element: " + collection.removeLast());
// Reversed collection
SequencedCollection<String> reversedCollection = collection.reversed();
System.out.println("Reversed Collection: " + reversedCollection);
}
}
Output:
First Element: First
Last Element: Third
Removed First Element: First
Removed Last Element: Third
Reversed Collection: [Second]
Example 2: Using SequencedCollection
with LinkedList
This example shows how to use a LinkedList
with the SequencedCollection
interface.
import java.util.LinkedList;
import java.util.SequencedCollection;
public class LinkedListSequencedCollectionExample {
public static void main(String[] args) {
SequencedCollection<Integer> collection = new LinkedList<>();
// Adding elements to the collection
collection.addFirst(1);
collection.addLast(2);
collection.addLast(3);
// Accessing the first and last elements
System.out.println("First Element: " + collection.getFirst());
System.out.println("Last Element: " + collection.getLast());
// Removing the first and last elements
System.out.println("Removed First Element: " + collection.removeFirst());
System.out.println("Removed Last Element: " + collection.removeLast());
// Reversed collection
SequencedCollection<Integer> reversedCollection = collection.reversed();
System.out.println("Reversed Collection: " + reversedCollection);
}
}
Output:
First Element: 1
Last Element: 3
Removed First Element: 1
Removed Last Element: 3
Reversed Collection: [2]
Example 3: Using SequencedCollection
with TreeSet
This example demonstrates how to use a TreeSet
with the SequencedCollection
interface.
import java.util.TreeSet;
import java.util.SequencedCollection;
public class TreeSetSequencedCollectionExample {
public static void main(String[] args) {
SequencedCollection<String> collection = new TreeSet<>();
// Adding elements to the collection
collection.add("Apple");
collection.add("Banana");
collection.add("Cherry");
// Accessing the first and last elements
System.out.println("First Element: " + collection.getFirst());
System.out.println("Last Element: " + collection.getLast());
// Removing the first and last elements
System.out.println("Removed First Element: " + collection.removeFirst());
System.out.println("Removed Last Element: " + collection.removeLast());
// Reversed collection
SequencedCollection<String> reversedCollection = collection.reversed();
System.out.println("Reversed Collection: " + reversedCollection);
}
}
Output:
First Element: Apple
Last Element: Cherry
Removed First Element: Apple
Removed Last Element: Cherry
Reversed Collection: [Banana]
4. Conclusion
The SequencedCollection
interface in Java, introduced in Java 21, provides a powerful way to manage collections 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 SequencedCollection
interface.
Comments
Post a Comment
Leave Comment