TreeMap class is a red-black tree implementation. It helps us in storing key-value pairs in sorted order. Here 3 approaches are discussed as follows where 4 key-value pairs in the TreeMap in all approaches are as follows along with syntax.
tree.put100, "=> Welcoming"); tree.put(120, "=> you to "); tree.put(140, "=> computer science portal"); tree.put(200, "=> Geeks for Geeks");
Approaches:
- Brute force method just to store TreeMap key-value pairs and display only.
- Using inbuilt functions: higherKey() method and lowerKey() method of TreeMap class.
- Using higherEntry().getValue() function and lowerEntry().getValue() function of TreeMap class.
Method 1: Storing key-value pairs in TreeMap and printing the key-value pairs.
Example:
Java
// Java Program to Get TreeMap Key and Value // Importing all classes // of java.util package import java.util.*; class GFG { // Main driver method public static void main(String[] args) { // Initialization of a TreeMap // using Generics TreeMap<Integer, String> tree = new TreeMap<Integer, String>(); // Inserting the Elements into TreeMap tree.put( 100 , "=> Welcoming" ); tree.put( 120 , "=> you to " ); tree.put( 140 , "=> computer science portal" ); tree.put( 200 , "=> Geeks for Geeks" ); // Iterating over TreeMap using for-each loop for (Map.Entry<Integer, String> map : tree.entrySet()) // Displaying all entries- keys and values // using getKey() and getValue() method System.out.println(map.getKey() + " " + map.getValue()); } } |
100 => Welcoming 120 => you to 140 => computer science portal 200 => Geeks for Geeks
Method 2: Printing the key that is greater than or less than the specified value by using inbuilt functions: higherKey() method and lowerKey() method of TreeMap class
The higherKey() method of java.util.TreeMap class is used to return the least key strictly greater than the given key, or null if there is no such key.
Syntax:
public K higherKey(K key)
Parameters: This method takes the key k as a parameter.
Return Value: This method returns the least key greater than the key, or null if there is no such key.
Exception: This method throws the NullPointerException if the specified key is null and this map uses natural ordering, or its comparator does not permit null keys.
The lowerKey() method is used to return the greatest key strictly less than to given key, passed as the parameter. In simpler words, this method is used to find the next greatest element after the element passed as the parameter.
Syntax:
public K TreeMap.lowerKey(K key)
Parameters: This method takes a mandatory parameter key which is the key to be matched.
Return Value: This method returns the greatest key strictly less than to key, or null if there is no such key.
Example:
Java
// Java Program to Get TreeMap Key, Value, then // Entry Greater or Less than the Specified Value // using lowerKey() and higherKey() of Tree class // Importing all classes of // java.util package import java.util.*; class GFG { // Main driver method public static void main(String[] args) { // Creating a TreeMap TreeMap<Integer, String> tree = new TreeMap<Integer, String>(); // Inserting the Elements into TreeMap tree.put( 100 , "=> Welcoming" ); tree.put( 120 , "=> you to " ); tree.put( 140 , "=> computer science portal" ); tree.put( 200 , "=> Geeks for Geeks" ); // Returning the smallest key among all the keys // greater than 150 System.out.println( "Smallest key among key > 150 : " + tree.higherKey( 150 )); // Returning the greatest key among all the keys // smaller than 150 System.out.println( "Greatest key among key < 150 : " + tree.lowerKey( 150 )); } } |
Smallest key among key > 150 : 200 Greatest key among key < 150 : 140
Method 3: Printing the value corresponding to the key that is greater than or less than the specified key via higherEntry().getValue() function and lowerEntry().getValue() function of TreeMap class
The higherEntry() method of java.util.TreeMap class is used to return a key-value mapping associated with the least key strictly greater than the given key, or null if there is no such key whereas the lowerEntry() method of java.util.TreeMap class is used to return a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
Example:
Java
// Java Program to Get TreeMap Key, Value, or // Entry Greater or Less than the Specified Value // using higherEntry().getValue() function and // lowerEntry().getValue() // Importing all classes of // java.util package import java.util.*; class GFG { // Main driver method public static void main(String[] args) { // Creating a TreeMap TreeMap<Integer, String> tree = new TreeMap<Integer, String>(); // Inserting the Elements into TreeMap tree.put( 100 , "=> Welcoming" ); tree.put( 120 , "=> you to " ); tree.put( 140 , "=> computer science portal" ); tree.put( 200 , "=> Geeks for Geeks" ); // Returning the value corresponding to the key // which is smallest among the keys greater than 150 System.out.println( tree.higherEntry( 150 ).getValue()); // Returning the value corresponding to the key // which is greatest among the keys smaller than 150 System.out.println(tree.lowerEntry( 150 ).getValue()); } } |
=> Geeks for Geeks => computer science portal