TreeMap floorKey() Method in Java with Examples

In this tutorial we will see TreeMap floorKey() Method in Java with Examples . TreeMap floorKey() method is used to return the greatest key less than or equal to given key into the parameter. Let’s first we Syntax of TreeMap floorKey method and then we will see different example of floorKey method .

Syntax:

public K floorKey(K key)

Parameters : This is the given key to be matched.

Return Value :  It’s return 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 floorKey() method – Return less than or equal Key

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

import java.util.TreeMap;

public class JavaTreeMapfloorKey {
	
	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 floorKey For key 10
    	System.out.println("treeMap.floorKey(10) : "+ treeMap.floorKey(10));
    	
    	//print floorKey For key 9
    	System.out.println("treeMap.floorKey(9) : "+ treeMap.floorKey(9));
    	
    	//print floorKey For key 6
    	System.out.println("treeMap.floorKey(6) : "+ treeMap.floorKey(6));
    	
    	//print floorKey For key 1
    	System.out.println("treeMap.floorKey(1) : "+ treeMap.floorKey(1));
    	
    	
	}

}

Output:

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

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

TreeMap floorKey() 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 floor(1) and floorKey(0) it will return null .

import java.util.TreeMap;

public class JavaTreeMapfloorKey1 {
	
	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.floorKey(1) : "+ treeMap.floorKey(1));
    	
    	System.out.println("treeMap.floorKey(0) : "+ treeMap.floorKey(0));
    	
	}

}

Output :

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

Example 3 : Java TreeMap floorKey() method – NullPointerException

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

import java.util.TreeMap;

public class JavaTreeMapfloorKey2 {
	
	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.floorKey(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 floorKey() method – ClassCastException

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

import java.util.TreeMap;

public class JavaTreeMapfloorKey3 {
	
	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.floorKey( 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 for Java TreeMap floorKey() method . You can see more java ThreeMap Example for practice .

Leave a Reply

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

5 + 4 =