TreeMap in Java are containers that store elements in a mapped fashion that is key-value and mapped value pair. Every element has a key–value and a respective mapped value. All the key values are unique and it is necessary that no two mapped values can have the same key value. It is based on Red-Black Tree implementation in which it does not matter in which order values are inserted in TreeMap, they remain sorted according to their key, unlike HashMap.
Syntax of TreeMap:
TreeMap<Integer, String> tree_map= new TreeMap<Integer, String>();
Syntax of SubMap:
Case 1: Method will include starting key but not include ending key
tree_map.submap(starting_key, ending_key);
Case 2: To include both keys, pass true with keys
tree_map.submap(starting_key, true, ending_key, true);
Case 3: To exclude any key, pass false with key
tree_map.submap(starting_key, false, ending_key, true);
Methods:
- Brute force method
- Using a predefined function
- Using defined comparator
- Using user-defined comparator
Method 1: Printing Default Sorted SubMap
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
TreeMap<Integer, String> my_map
= new TreeMap<Integer, String>();
my_map.put( 5 , "DoubleAlpha" );
my_map.put( 1 , "Alpha" );
my_map.put( 3 , "Beta" );
my_map.put( 2 , "Gamma" );
my_map.put( 4 , "Theta" );
System.out.println(
"Elements: "
+ my_map.subMap( 1 , true , 5 , true ));
}
}
|
OutputElements: {1=Alpha, 2=Gamma, 3=Beta, 4=Theta, 5=DoubleAlpha}
Method 2: Reversed sorted Map using predefined functions
Example:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
TreeMap<Integer, String> my_map
= new TreeMap<Integer, String>();
my_map.put( 5 , "DoubleAlpha" );
my_map.put( 1 , "Alpha" );
my_map.put( 3 , "Beta" );
my_map.put( 2 , "Gamma" );
my_map.put( 4 , "Theta" );
Map<Integer, String> reversed_map
= my_map.descendingMap();
System.out.println( "Elements: " + reversed_map);
}
}
|
OutputElements: {5=DoubleAlpha, 4=Theta, 3=Beta, 2=Gamma, 1=Alpha}
Method 3: Printing Sorted Sub-Map using User Defined Comparator
The idea behind the user-defined comparator is that if the user wants to sort the map according to the user’s preference. Compare the function of the comparator is used to implement our sorting logic.
Example:
Java
import java.util.*;
public class GFG {
public static void main(String a[])
{
TreeMap<String, String> my_map
= new TreeMap<String, String>( new UserComp());
my_map.put( "DoubleAlpha" , "RedDotGeek" );
my_map.put( "Alpha" , "GeekAmongGeek" );
my_map.put( "Beta" , "MasterGeek" );
my_map.put( "Gamma" , "Geek" );
my_map.put( "Theta" , "OnwayGeek" );
System.out.println(
"Elements: "
+ my_map.subMap( "A" , true , "Z" , true ));
}
}
class UserComp implements Comparator<String> {
public int compare(String a, String b)
{
return a.compareTo(b);
}
}
|
OutputElements: {Alpha=GeekAmongGeek, Beta=MasterGeek, DoubleAlpha=RedDotGeek, Gamma=Geek, Theta=OnwayGeek}
Method 4: Reversed Sorted Map using User Defined Comparator
Java
import java.util.*;
public class GFG {
public static void main(String a[])
{
TreeMap<Integer, String> my_map
= new TreeMap<Integer, String>( new UserComp());
my_map.put( 1 , "DoubleAlpha" );
my_map.put( 8 , "Alpha" );
my_map.put( 5 , "Beta" );
my_map.put( 3 , "Gamma" );
my_map.put( 4 , "Theta" );
System.out.println( "Elements : " + my_map);
}
}
class UserComp implements Comparator<Integer> {
public int compare(Integer a, Integer b)
{
if (b > a)
return 1 ;
return - 1 ;
}
}
|
OutputElements : {8=Alpha, 5=Beta, 4=Theta, 3=Gamma, 1=DoubleAlpha}