Java.util.TreeMap.floorEntry() and floorKey() in Java

Last Updated : 24 Nov, 2017
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.




// Java code to demonstrate the working
// of floorEntry()
import java.io.*;
import java.util.*;
public class floorEntry {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap<Integer, String> tmp = new TreeMap<Integer, String>();
  
        // assigning the values in the tree map
        // using put()
        tmp.put(10, "ten");
        tmp.put(7, "seven");
        tmp.put(19, "nineteen");
        tmp.put(3, "three");
  
        // use of floorEntry()
        // displays the floor value of 6
        // prints 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.




// Java code to demonstrate the working
// of floorKey()
import java.io.*;
import java.util.*;
public class floorKey {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap<Integer, String> tmp = new TreeMap<Integer, String>();
  
        // assigning the values in the tree map
        // using put()
        tmp.put(10, "ten");
        tmp.put(7, "seven");
        tmp.put(19, "nineteen");
        tmp.put(3, "three");
  
        // use of floorKey()
        // displays the floor key of
        // prints 3
        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.




// Java code to demonstrate the application
// of floorKey() and floorEntry
import java.io.*;
import java.util.*;
public class floorappli {
    public static void main(String[] args)
    {
  
        // Declaring the tree map of Integer and String
        TreeMap<Integer, String> destin = new TreeMap<Integer, String>();
  
        // assigning the km with destination
        // from beginning
        // using put()
        destin.put(10, "Delhi");
        destin.put(7, "Gurgaon");
        destin.put(19, "Noida");
        destin.put(3, "Ring Road");
  
        // Entering the available Petrol (consider 1km/litre)
        int petr = 12;
  
        // Maximum place you can reach
        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



Similar Reads

TreeMap floorKey() in Java with Examples
Pre-requisite: TreeMap in Java The floorKey() method is used to return the greatest key less than or equal to given key from the parameter. Syntax: public K floorKey(K key) Parameter: This method accepts a mandatory parameter key which is the key to be matched. Return Value: The method call returns the greatest key less than or equal to key, or nul
2 min read
TreeMap floorEntry() Method in Java With Examples
The java.util.TreeMap.floorEntry() method is used to return 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. Syntax: tree_map.floorEntry(K key) Parameters: This method takes one parameter key to be matched while mapping. Return Value: This method returns an entry with the gre
2 min read
NavigableMap floorKey() method in Java
The floorKey() method of NavigableMap interface in Java is used to return the greatest key less than or equal to the given key, or null if there is no such key. Syntax: K floorKey(K key) Where, K is the type of key maintained by this map. Parameters: This function accepts a single parameter Key which refers to the type of key maintained by this map
2 min read
NavigableMap floorEntry() method in Java
The floorEntry() method of NavigableMap interface in Java is used to return 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. Syntax: Map.Entry<K, V> floorEntry(K key) Where, key is the key maintained by this map. Parameters: key - the key Return Value: It returns an ent
2 min read
Java.util.TreeMap.pollFirstEntry() and pollLastEntry() in Java
Java.util.TreeMap also contains functions that support retrieval and deletion at both, high and low end of values and hence give a lot of flexibility in applicability and daily use. This function is poll() and has 2 variants discussed in this article. 1. pollFirstEntry() : It removes and retrieves a key-value pair with the least key value in the ma
4 min read
Java.util.TreeMap.descendingMap() and descendingKeyset() in Java
There are two variants of descending() in Java.util.TreeMap, both are discussed in this article. 1. descendingKeySet(): It returns a reverse order Navigable Set view of the keys contained in the map. Syntax : public NavigableSet descendingKeySet() Parameters: NA Return Value: It returns a reverse order navigable set view of the keys in this map. Ex
3 min read
Java.util.TreeMap.containskey() and containsValue() in Java
There are two variants of contains() in Java.util.TreeMap, both are discussed in this article. 1. containskey(Object o) : It returns true if the map contains a mapping for the specified key. Parameters: o : The key which will be tested whether present or not. Return Value: Returns true if there is a mapping for the given key. Exception: ClassCastEx
2 min read
Java.util.TreeMap.put() and putAll() in Java
The insertion in TreeMap are handled using the variants of the put functions. There are two variants of put() in Java.util.TreeMap, both are discussed in this article. 1. put() : It associates the specified value with the specified key in the map. If a key is already present, then updating it results in updation of that key. Parameters: key : The k
4 min read
Similarities Between TreeMap and TreeSet in Java
TreeSet is mainly an implementation of SortedSet in java where duplication is not allowed and objects are stored in sorted and ascending order. TreeMap is an implementation of Map Interface . TreeMap is also Implementation of NavigableMap along with AbstractMap class. Similarities between TreeSet and TreeMap in java. Both TreeMap and TreeSet belong
1 min read
Difference between TreeMap and TreeSet in Java
TreeSet is mainly an implementation of SortedSet in java where duplication is not allowed and objects are stored in sorted and ascending order. Some important features of the TreeSet are: In TreeSet duplicate values are not allowed because it implements the SortedSet interface.Objects in a TreeSet are stored in ascending order.In TreeSet the insert
2 min read
HashMap and TreeMap in Java
HashMap and TreeMap are part of collection framework. HashMapjava.util.HashMap class is a Hashing based implementation. In HashMap, we have a key and a value pair<Key, Value>. HashMap<K, V> hmap = new HashMap<K, V>(); Let us consider below example where we have to count occurrences of each integer in given array of integers. Input
5 min read
Differences between TreeMap, HashMap and LinkedHashMap in Java
Prerequisite : HashMap and TreeMap in Java TreeMap, HashMap and LinkedHashMap: What's Similar? All offer a key->value map and a way to iterate through the keys. The most important distinction between these classes is the time guarantees and the ordering of the keys.All three classes HashMap, TreeMap and LinkedHashMap implements java.util.Map int
5 min read
TreeMap ceilingEntry() and ceilingKey() methods in Java
There are two variants of ceilingEntry() in Java.util.TreeMap, both are discussed in this article. 1. ceilingEntry(K Key) : It is used to return a key-value mapping associated with the least key greater than or equal to the given key, or null if there is no such key. Syntax : public Map.Entry ceilingEntry(K key) Parameters : key : The key to be mat
3 min read
TreeMap firstEntry() and firstKey() Method in Java with Examples
There are two variants of first() in Java.util.TreeMap, both are discussed in this article. Method 1: firstEntry() It returns a key-value mapping associated with the least key in this map, or null if the map is empty. Syntax: public Map.Entry firstEntry() Return Type: An entry with the least key and null if the map is empty. Example: Java Code // J
3 min read
TreeMap containsValue() Method in Java With Examples
In Java, containsValue() method of the TreeMap class is used to check whether a particular value is being mapped by any key in the TreeMap. It takes the value as a parameter and returns True if that value is mapped by any of the keys in the map. --> java.util Package --> TreeMap class --> containsValue() Method Syntax: Tree_Map.containsVal
3 min read
TreeMap size() Method in Java with Examples
size() method of TreeMap class is used to get the size of the map which refers to the number of the key-value pair or mappings in the Map. --> java.util package --> TreeMap class --> size() Method Syntax: Tree_Map.size() Return Value: The size of the map which also means the number of key-value pairs present in the map. Example 1: Mapping
2 min read
TreeMap get() Method in Java
The java.util.TreeMap.get() method of TreeMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. Syntax: Tree_Map.get(Object key_element) Parameter: The method takes one parameter key_element of object type and refers to the key whose
2 min read
TreeMap clone() Method in Java with Examples
In Java, clone() method of the TreeMap class is used to return a shallow copy of the mentioned treemap. It just creates a copy of the map. --> java.util Package --> TreeMap Class --> clone() Method Syntax: Tree_Map.clone() Parameters: The method does not take any parameters. Return Type: A copy of the TreeMap. Example 1: Mapping string val
2 min read
TreeMap containsKey() Method in Java
The java.util.TreeMap.containsKey() method is used to check whether a particular key is being mapped in the TreeMap or not. It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: Tree_Map.containsKey(key_element) Parameters: The method takes just one parameter key_element that refers to the key whose
2 min read
TreeMap clear() Method in Java
The java.util.TreeMap.clear() method in Java is used to clear and remove all of the elements or mappings from a specified TreeMap. Syntax: Tree_Map.clear() Parameters: The method does not accept any parameters. Return Value: The method does not return any value. Below programs are used to illustrate the working of java.util.TreeMap.clear() Method:
2 min read
TreeMap headMap() Method in Java
The java.util.TreeMap.headMap(key_point) method of TreeMap class is used to get all the pairs or portion of the map strictly less than the parameter key_value. The mentioned parameter is excluded from the newly prepared treemap. Since the set is backed by the map, so any changes to the map are reflected in the other map, and vice-versa. Syntax: sor
3 min read
TreeMap entrySet() Method in Java
The java.util.TreeMap.entrySet() method in Java is used to create a set out of the same elements contained in the treemap. It basically returns a set view of the treemap or we can create a new set and store the map elements into them. Syntax: tree_map.entrySet() Parameters: The method does not take any parameter. Return Value: The method returns a
2 min read
TreeMap keySet() Method in Java with Examples
In Java, keySet() method of TreeMap class is present inside java.util package in Java is used to create a set out of the key elements contained in the treemap. It basically returns a set view of the keys or we can create a new set and store the key elements in them in ascending order. Since the set is backed by the map, any changes made to the map
3 min read
TreeMap lastKey() Method in Java
The java.util.TreeMap.lastKey() is used to retrieve the last or the highest key present in the map. Syntax: tree_map.lastKey() Parameters: The method does not take any parameters. Return Value: The method returns the last key present in the map. Exception: The method throws NoSuchElementException if the map is empty. Below programs illustrate the w
2 min read
TreeMap put() Method in Java
The java.util.TreeMap.put() method of TreeMap is used to insert a mapping into a map. This means we can insert a specific key and the value it is mapping to into a particular map. If an existing key is passed then the previous value gets replaced by the new value. If a new pair is passed, then the pair gets inserted as a whole. Syntax: Tree_Map.put
3 min read
TreeMap putAll() Method in Java
The java.util.TreeMap.putAll() is an inbuilt method of TreeMap class that is used for the copy operation. The method copies all of the elements i.e., the mappings, from one map into another. Syntax: new_tree_map.putAll(exist_tree_map) Parameters: The method takes one parameter exist_tree_map that refers to the existing map we want to copy from. Ret
2 min read
TreeMap remove() Method in Java
The java.util.TreeMap.remove() is an inbuilt method of TreeMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. Syntax: Tree_Map.remove(Object key) Parameters: The method takes one parameter key whose mapping is to be removed from the Map. Return Value: Th
2 min read
TreeMap values() Method in Java with Examples
In Java, the values() method of the TreeMap class is present inside java.util package which is used to create a collection out of the values of the map. It basically returns a Collection view of the values in the TreeMap. --> java.util package --> TreeMap class --> values() Method Syntax: Tree_Map.values() Return Type: A collection view co
2 min read
TreeMap subMap() Method in Java with Examples
In Java, subMap() method of TreeMap class is used to return the part or portion of the map defined by the specified range of keys in the parameter. Any changes made in one or the other map will reflect the change in the other map. Syntax: Tree_Map.subMap(K startKey, K endKey) Parameters: The method takes two parameters of Key type: The starting poi
3 min read
TreeMap lowerKey() in Java with Examples
Pre-requisite: TreeMap in Java 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 parame
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg