Java.util.TreeMap.floorEntry() and floorKey() in Java
Last Updated :
24 Nov, 2017
Finding greatest number less than given value is used in many a places and having that feature in a map based container is always a plus. Java.util.TreeMap also offers this functionality using floor() function. There are 2 variants, both are discussed below.
1. floorEntry() : It returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.
Parameters:
key : This is the key to be matched.
Return Value:
It returns an entry with the greatest key less than or equal to key,
or null if there is no such key.
Exception:
ClassCastException : This exception is thrown if the specified
key cannot be compared with the keys currently in the map.
NullPointerException : This exception is thrown if the specified
key is null and this map uses natural ordering, or its comparator does not permit null keys.
import java.io.*;
import java.util.*;
public class floorEntry {
public static void main(String[] args)
{
TreeMap<Integer, String> tmp = new TreeMap<Integer, String>();
tmp.put( 10 , "ten" );
tmp.put( 7 , "seven" );
tmp.put( 19 , "nineteen" );
tmp.put( 3 , "three" );
System.out.println( "The greatest key-value less than 6 is : "
+ tmp.floorEntry( 6 ));
}
}
|
Output:
The greatest key-value less than 6 is : 3=three
2. floorKey() : It returns the greatest key less than or equal to the given key, or null if there is no such key.
Parameters:
key : This is the key to be matched.
Return Value:
It returns an entry with the greatest key less than or equal to key, or
null if there is no such key.
Exception:
ClassCastException : This exception is thrown if the specified
key cannot be compared with the keys currently in the map.
NullPointerException : This exception is thrown if the specified key
is null and this map uses natural ordering, or its comparator does not permit null keys.
import java.io.*;
import java.util.*;
public class floorKey {
public static void main(String[] args)
{
TreeMap<Integer, String> tmp = new TreeMap<Integer, String>();
tmp.put( 10 , "ten" );
tmp.put( 7 , "seven" );
tmp.put( 19 , "nineteen" );
tmp.put( 3 , "three" );
System.out.println( "The greatest key less than 6 is : "
+ tmp.floorKey( 6 ));
}
}
|
Output:
The greatest key less than 6 is : 3
Practical Application : The possible applications of this functions are aplenty. Starting from maximum distance with given petrol or possible dishes with given ingredients. Former one is discussed in code below.
import java.io.*;
import java.util.*;
public class floorappli {
public static void main(String[] args)
{
TreeMap<Integer, String> destin = new TreeMap<Integer, String>();
destin.put( 10 , "Delhi" );
destin.put( 7 , "Gurgaon" );
destin.put( 19 , "Noida" );
destin.put( 3 , "Ring Road" );
int petr = 12 ;
System.out.println( "The maximum place you can reach with given petrol : "
+ destin.floorEntry(petr));
}
}
|
Output:
The maximum place you can reach with given petrol : 10=Delhi