The TreeMap.subMap()
method in Java allows you to retrieve a view of a portion of a TreeMap
based on a range of keys. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how TreeMap.subMap()
can be used effectively.
Table of Contents
- Introduction
subMap
Method SyntaxNavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
SortedMap<K,V> subMap(K fromKey, K toKey)
- Examples
- Using
NavigableMap.subMap()
- Using
SortedMap.subMap()
- Using
- Real-World Use Case
- Example: Retrieving a Subset of Contacts
- Conclusion
Introduction
The TreeMap.subMap()
method is a member of the TreeMap
class in Java. It provides a way to obtain a view of a specific portion of the map based on key ranges. This method can be used to create a subset of the map with keys that fall within a specified range.
subMap() Method Syntax
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
This method returns a view of the portion of this map whose keys range from fromKey
to toKey
.
Syntax
public NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)
- Parameters:
fromKey
: The low endpoint of the keys in the returned map.fromInclusive
:true
if the low endpoint is to be included in the returned view.toKey
: The high endpoint of the keys in the returned map.toInclusive
:true
if the high endpoint is to be included in the returned view.
- Returns: A
NavigableMap
view of the specified portion of this map.
SortedMap<K,V> subMap(K fromKey, K toKey)
This method returns a view of the portion of this map whose keys range from fromKey
, inclusive, to toKey
, exclusive.
Syntax
public SortedMap<K,V> subMap(K fromKey, K toKey)
- Parameters:
fromKey
: The low endpoint (inclusive) of the keys in the returned map.toKey
: The high endpoint (exclusive) of the keys in the returned map.
- Returns: A
SortedMap
view of the specified portion of this map.
Examples
Using NavigableMap.subMap()
Example
import java.util.TreeMap;
import java.util.NavigableMap;
public class NavigableMapSubMapExample {
public static void main(String[] args) {
// Creating a TreeMap with String keys and Integer values
TreeMap<String, Integer> treeMap = new TreeMap<>();
// Adding entries to the TreeMap
treeMap.put("Ravi", 25);
treeMap.put("Priya", 30);
treeMap.put("Vijay", 35);
treeMap.put("Anita", 28);
treeMap.put("Suresh", 40);
// Getting a submap from "Priya" (inclusive) to "Vijay" (inclusive)
NavigableMap<String, Integer> subMap = treeMap.subMap("Priya", true, "Vijay", true);
// Printing the submap
System.out.println("SubMap from 'Priya' to 'Vijay': " + subMap);
}
}
Output:
SubMap from 'Priya' to 'Vijay': {Priya=30, Ravi=25, Vijay=35}
Using SortedMap.subMap()
Example
import java.util.TreeMap;
import java.util.SortedMap;
public class SortedMapSubMapExample {
public static void main(String[] args) {
// Creating a TreeMap with String keys and Integer values
TreeMap<String, Integer> treeMap = new TreeMap<>();
// Adding entries to the TreeMap
treeMap.put("Ravi", 25);
treeMap.put("Priya", 30);
treeMap.put("Vijay", 35);
treeMap.put("Anita", 28);
treeMap.put("Suresh", 40);
// Getting a submap from "Priya" (inclusive) to "Vijay" (exclusive)
SortedMap<String, Integer> subMap = treeMap.subMap("Priya", "Vijay");
// Printing the submap
System.out.println("SubMap from 'Priya' to 'Vijay' (exclusive): " + subMap);
}
}
Output:
SubMap from 'Priya' to 'Vijay' (exclusive): {Priya=30, Ravi=25}
Real-World Use Case
Example: Retrieving a Subset of Contacts
A common real-world use case for TreeMap.subMap()
is managing a contact list where you need to retrieve a subset of contacts based on their names.
Example
import java.util.TreeMap;
import java.util.NavigableMap;
public class ContactManager {
public static void main(String[] args) {
// Creating a TreeMap to manage contacts
TreeMap<String, Integer> contacts = new TreeMap<>();
// Adding contacts to the TreeMap
contacts.put("Ravi", 1234567890);
contacts.put("Priya", 9876543210);
contacts.put("Vijay", 5556667777);
contacts.put("Anita", 4445556666);
contacts.put("Suresh", 6667778888);
// Retrieving a subset of contacts from "Priya" (inclusive) to "Vijay" (inclusive)
NavigableMap<String, Integer> subContacts = contacts.subMap("Priya", true, "Vijay", true);
// Printing the subset of contacts
System.out.println("Subset of contacts from 'Priya' to 'Vijay': ");
for (Map.Entry<String, Integer> entry : subContacts.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
Output:
Subset of contacts from 'Priya' to 'Vijay':
Priya: 9876543210
Ravi: 1234567890
Vijay: 5556667777
In this example, TreeMap.subMap()
is used to retrieve a subset of contacts based on their names, making it easy to manage and view a specific portion of the contact list.
Conclusion
The TreeMap.subMap()
method in Java provides a way to obtain a view of a portion of the map based on key ranges. By understanding how to use this method, you can efficiently manage collections of key-value pairs in your Java applications. The method allows you to create subsets of the map, making it a versatile tool for data management in various scenarios.