Introduction
The SequencedMap
interface in Java, introduced in Java 21, is part of the java.util
package. It extends the Map
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
SequencedMap
Interface? - Common Methods
- Examples of Using the
SequencedMap
Interface - Conclusion
1. What is the SequencedMap Interface?
The SequencedMap
interface extends the Map
interface and provides methods to access elements based on their sequence within the map. This interface is useful for maps 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 aSequencedMap
in reverse order.putFirst(K key, V value)
: Inserts the specified key-value pair at the beginning of the map.putLast(K key, V value)
: Appends the specified key-value pair to the end of the map.firstEntry()
: Returns the first key-value pair in the map.lastEntry()
: Returns the last key-value pair in the map.pollFirstEntry()
: Removes and returns the first key-value pair from the map.pollLastEntry()
: Removes and returns the last key-value pair from the map.
3. Examples of Using the SequencedMap Interface
Example 1: Basic Usage with LinkedHashMap
This example demonstrates how to use a LinkedHashMap
with the SequencedMap
interface.
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SequencedMap;
public class SequencedMapExample {
public static void main(String[] args) {
SequencedMap<String, Integer> map = new LinkedHashMap<>();
// Adding elements to the map
map.putFirst("First", 1);
map.putLast("Second", 2);
map.putLast("Third", 3);
// Accessing the first and last entries
System.out.println("First Entry: " + map.firstEntry());
System.out.println("Last Entry: " + map.lastEntry());
// Removing the first and last elements
Map.Entry<String, Integer> removedFirst = map.pollFirstEntry();
System.out.println("Removed First: " + removedFirst.getKey() + "=" + removedFirst.getValue());
Map.Entry<String, Integer> removedLast = map.pollLastEntry();
System.out.println("Removed Last: " + removedLast.getKey() + "=" + removedLast.getValue());
// Reversed map
SequencedMap<String, Integer> reversedMap = map.reversed();
System.out.println("Reversed Map: " + reversedMap);
}
}
Output:
First Entry: First=1
Last Entry: Third=3
Removed First: First=1
Removed Last: Third=3
Reversed Map: {Second=2}
Example 2: Using SequencedMap
with TreeMap
This example shows how to use a TreeMap
with the SequencedMap
interface.
import java.util.Map;
import java.util.SequencedMap;
import java.util.TreeMap;
public class TreeMapSequencedMapExample {
public static void main(String[] args) {
SequencedMap<String, Integer> map = new TreeMap<>();
// Adding elements to the map
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Cherry", 3);
// Accessing the first and last entries
System.out.println("First Entry: " + map.firstEntry());
System.out.println("Last Entry: " + map.lastEntry());
// Removing the first and last elements
Map.Entry<String, Integer> removedFirst = map.pollFirstEntry();
System.out.println("Removed First: " + removedFirst.getKey() + "=" + removedFirst.getValue());
Map.Entry<String, Integer> removedLast = map.pollLastEntry();
System.out.println("Removed Last: " + removedLast.getKey() + "=" + removedLast.getValue());
// Reversed map
SequencedMap<String, Integer> reversedMap = map.reversed();
System.out.println("Reversed Map: " + reversedMap);
}
}
Output:
First Entry: Apple=1
Last Entry: Cherry=3
Removed First: Apple=1
Removed Last: Cherry=3
Reversed Map: {Banana=2}
Example 3: Using SequencedMap
with ConcurrentSkipListMap
This example demonstrates how to use a ConcurrentSkipListMap
with the SequencedMap
interface.
import java.util.Map;
import java.util.SequencedMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class ConcurrentSkipListMapSequencedMapExample {
public static void main(String[] args) {
SequencedMap<String, Integer> map = new ConcurrentSkipListMap<>();
// Adding elements to the map
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
// Accessing the first and last entries
System.out.println("First Entry: " + map.firstEntry());
System.out.println("Last Entry: " + map.lastEntry());
// Removing the first and last elements
Map.Entry<String, Integer> removedFirst = map.pollFirstEntry();
System.out.println("Removed First: " + removedFirst.getKey() + "=" + removedFirst.getValue());
Map.Entry<String, Integer> removedLast = map.pollLastEntry();
System.out.println("Removed Last: " + removedLast.getKey() + "=" + removedLast.getValue());
// Reversed map
SequencedMap<String, Integer> reversedMap = map.reversed();
System.out.println("Reversed Map: " + reversedMap);
}
}
Output:
First Entry: One=1
Last Entry: Two=2
Removed First: One=1
Removed Last: Two=2
Reversed Map: {Three=3}
4. Conclusion
The SequencedMap
interface in Java, introduced in Java 21, provides a powerful way to manage maps 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 SequencedMap
interface.
Comments
Post a Comment
Leave Comment