Java Program to Get TreeMap Key, Value, or Entry Greater or Less than the Specified Value
Last Updated :
07 Jan, 2021
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
TreeMap<Integer, String> tree
= new TreeMap<Integer, String>();
tree.put( 100 , "=> Welcoming" );
tree.put( 120 , "=> you to " );
tree.put( 140 , "=> computer science portal" );
tree.put( 200 , "=> Geeks for Geeks" );
for (Map.Entry<Integer, String> map :
tree.entrySet())
System.out.println(map.getKey() + " "
+ map.getValue());
}
}
|
Output
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
TreeMap<Integer, String> tree
= new TreeMap<Integer, String>();
tree.put( 100 , "=> Welcoming" );
tree.put( 120 , "=> you to " );
tree.put( 140 , "=> computer science portal" );
tree.put( 200 , "=> Geeks for Geeks" );
System.out.println( "Smallest key among key > 150 : "
+ tree.higherKey( 150 ));
System.out.println( "Greatest key among key < 150 : "
+ tree.lowerKey( 150 ));
}
}
|
Output
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
TreeMap<Integer, String> tree
= new TreeMap<Integer, String>();
tree.put( 100 , "=> Welcoming" );
tree.put( 120 , "=> you to " );
tree.put( 140 , "=> computer science portal" );
tree.put( 200 , "=> Geeks for Geeks" );
System.out.println(
tree.higherEntry( 150 ).getValue());
System.out.println(tree.lowerEntry( 150 ).getValue());
}
}
|
Output
=> Geeks for Geeks
=> computer science portal
Share your thoughts in the comments
Please Login to comment...