TreeMap floorEntry() method in Java

In this tutorial we will learn about treemap floorEntry() method in java. TreeMap floorEntry() method is used to return a key-value mapping associated with the greatest key less than or equal to the given key . Let’s first we Syntax of TreeMap floorEntry() method and then we will see different example of floorEntry method .

Syntax :

public K floorEntry(K key)

Parameters : This is the given 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 : This method throws following exceptions .

ClassCastException – if the given key cannot be compared with the keys currently in the map
NullPointerException – if the given key is null and this map uses natural ordering, or its comparator does not permit null keys.

Example 1 : Java TreeMap floorEntry() method – Return less than or equal Key

In this example we will create TreeMap and add some element in it . Now we use floorEntry() method with 10 ,9,6 and 1 key . Because here maximum value of key is 9 , So for floorKey(10) it returns 9=php . for floorKey(9) it returns 9=php , because key value 9 is present in TreeMap . for floorKey(6) , its return 5=map  and floorKey(1) , it return 1 =java.

import java.util.TreeMap;

public class TreeMapfloorEntry {
	
	public static void main(String[] args) {
		TreeMap<Integer,String> treeMap = new TreeMap<Integer,String>();

    	treeMap.put(1,"java");
    	treeMap.put(2, "vogue");
    	treeMap.put(3,"welcomes");
    	treeMap.put(4,"you");
    	treeMap.put(5,"map");
    	treeMap.put(9,"php");
    	
        //print TreeMap Elements 
    	System.out.println("TreeMap Elements : "+ treeMap);

    	//print floorEntry For key 10
    	System.out.println("treeMap.floorEntry(10) : "+ treeMap.floorEntry(10));
    	
    	//print floorEntry For key 9
    	System.out.println("treeMap.floorEntry(9) : "+ treeMap.floorEntry(9));
    	
    	//print floorEntry For key 6
    	System.out.println("treeMap.floorEntry(6) : "+ treeMap.floorEntry(6));
    	
    	//print floorEntry For key 1
    	System.out.println("treeMap.floorEntry(1) : "+ treeMap.floorEntry(1));

		
	}

}

Output:

TreeMap Elements : {1=java, 2=vogue, 3=welcomes, 4=you, 5=map, 9=php}
treeMap.floorEntry(10) : 9=php
treeMap.floorEntry(9) : 9=php
treeMap.floorEntry(6) : 5=map
treeMap.floorEntry(1) : 1=java

Example 2: Java TreeMap floorEntry() method – Return null key

TreeMap floorEntry() method will return null key if there is no less than or equal key of given key . Because minimum value of key is 2 , So for floorEntry(1) and floorEntry(0) it will return null .

import java.util.TreeMap;

public class TreeMapfloorEntry1 {

	public static void main(String[] args) {

		TreeMap<Integer, String> treeMap = new TreeMap<Integer, String>();

		treeMap.put(2, "java");
		treeMap.put(3, "vogue");
		treeMap.put(4, "welcomes");
		treeMap.put(5, "you");
		treeMap.put(6, "map");
		treeMap.put(9, "php");

		// print TreeMap Elements
		System.out.println("TreeMap Elements : " + treeMap);

		System.out.println("treeMap.floorEntry(1) : " + treeMap.floorEntry(1));

		System.out.println("treeMap.floorEntry(0) : " + treeMap.floorEntry(0));

	}

}

Output:

TreeMap Elements : {2=java, 3=vogue, 4=welcomes, 5=you, 6=map, 9=php}
treeMap.floorEntry(1) : null
treeMap.floorEntry(0) : null

Example 3 : Java TreeMap floorEntry() method – NullPointerException

We will use null key then TreeMap floorEntry() method will throw null pointer exception

import java.util.TreeMap;

public class TreeMapfloorEntry2 {
	
	public static void main(String[] args) {
		
		TreeMap<Integer,String> treeMap = new TreeMap<Integer,String>();

    	treeMap.put(2,"java");
    	treeMap.put(3, "vogue");
    	treeMap.put(4,"welcomes");
    	treeMap.put(5,"you");
    	treeMap.put(6,"map");
    	treeMap.put(9,"php");
    	
       //print TreeMap elements 
    	System.out.println("treeMap : "+ treeMap);
    	try{
    	System.out.println(" "+ treeMap.floorEntry(null));
    	}catch(Exception e){
    		System.out.println("Exception : "+ e);
    	}
    	
	}

}

Output:

treeMap : {2=java, 3=vogue, 4=welcomes, 5=you, 6=map, 9=php}
Exception : java.lang.NullPointerException

Example 4 : Java TreeMap floorEntry() method – ClassCastException

Java TreeMap floorEntry() method throws ClassCastException if the given key cannot be compared with the keys in the map . Now let’s see java treemap floorEntry example for ClassCastException .

import java.util.TreeMap;

public class TreeMapfloorEntry3 {

	public static void main(String[] args) {
		
        TreeMap<Object,String> treeMap = new TreeMap<Object,String>();

    	treeMap.put(2,"java");
    	treeMap.put(3, "vogue");
    	treeMap.put(4,"welcomes");
    	treeMap.put(5,"you");
    	treeMap.put(6,"map");
    	treeMap.put(9,"php");
    	
       //print TreeMap elements 
    	System.out.println("treeMap : "+ treeMap);
    	try{
    	System.out.println(" "+ treeMap.floorEntry( new String("JavaMap") ));
    	}catch(Exception e){
    		System.out.println("Exception : "+ e);
    	}
	}
}

Output:

treeMap : {2=java, 3=vogue, 4=welcomes, 5=you, 6=map, 9=php}
Exception : java.lang.ClassCastException: 
java.lang.Integer cannot be cast to java.lang.String

In this tutorial we have learned different example of treemap floorentry() method in java . You can see more java TreeMap program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

32 + = 42