This example shows how to get the key, value, or an entry greater than or less than the specified key from the TreeMap in Java using the higherKey, higherEntry, lowerKey, lowerEntry, ceilingKey, ceilingEntry, floorKey, and floorEntry methods.
How to get a key from TreeMap that is less than the specified key?
There are a couple of ways using which we can get the key that is less than the specified key from the TreeMap.
1. Using the lowerKey method
The lowerKey
method returns the greatest key from the TreeMap which is less than the specified key.
1 |
public K lowerKey(K key) |
It returns null if there is no such key in the TreeMap object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.TreeMap; public class TreeMapGetHigherLowerKeysExample { public static void main(String[] args) { TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(3, "Three"); tmapNumbers.put(5, "Five"); tmapNumbers.put(7, "Seven"); tmapNumbers.put(9, "Nine"); /* * To get the key that is less than the * specified key use the lowerKey method */ //this will return 1, i.e. key < 3 System.out.println( tmapNumbers.lowerKey(3) ); //this will return null as there is no key which is < 1 System.out.println( tmapNumbers.lowerKey(1) ); } } |
Output
1 2 |
1 null |
2. Using the floorkey method
The floorKey
method returns the greatest key from the TreeMap which is less than or equal to the specified key.
1 |
public K floorKey(K key) |
It returns null if there is no such key in the TreeMap object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(3, "Three"); tmapNumbers.put(5, "Five"); tmapNumbers.put(7, "Seven"); tmapNumbers.put(9, "Nine"); /* * To get the key that is less than or equal to the * specified key use the floorKey method */ //this will return 3, i.e. key <= 3 System.out.println( tmapNumbers.floorKey(3) ); //this will return 1, i.e. key <=1 System.out.println( tmapNumbers.floorKey(1) ); /* * this will return null as there is no key which is * less than or equal to the 0 */ System.out.println( tmapNumbers.floorKey(0) ); |
You can also use the lowerEntry
and floorEntry
methods instead of the lowerKey
and floorKey
methods respectively to get an entry instead of the key.
To get a value, get the entry and then use the getValue
method of the Map.Entry to get value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(3, "Three"); tmapNumbers.put(5, "Five"); tmapNumbers.put(7, "Seven"); tmapNumbers.put(9, "Nine"); //this will return entry having key < 3 System.out.println( tmapNumbers.lowerEntry(3) ); //or get value if(tmapNumbers.lowerEntry(3) != null) System.out.println( tmapNumbers.lowerEntry(3).getValue() ); |
Output
1 2 |
1=One One |
How to get a key from TreeMap that is greater than the specified key?
There are a couple of ways using which we can get the key that is greater than the specified key from the TreeMap.
1. Using the higherKey method
The higherKey
method returns the lowest key from the TreeMap that is greater than the specified key.
1 |
public K higherKey(K key) |
It returns null if there is no such key in the TreeMap object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(3, "Three"); tmapNumbers.put(5, "Five"); tmapNumbers.put(7, "Seven"); tmapNumbers.put(9, "Nine"); /* * To get the key that is greater than the specified * key, use the higherKey method */ //this will return 9, i.e. key > 7 System.out.println( tmapNumbers.higherKey(7) ); //this will return null as there is no key which is > 9 System.out.println( tmapNumbers.higherKey(9) ); |
Output
1 2 |
9 null |
2. Using the ceilingKey method
The ceilingKey
method returns the lowest key from the TreeMap that is greater than or equal to the specified key.
1 |
public K ceilingKey(K key) |
It returns null if there is no such key in the TreeMap object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(3, "Three"); tmapNumbers.put(5, "Five"); tmapNumbers.put(7, "Seven"); tmapNumbers.put(9, "Nine"); /* * To get the key that is greater than or equal to * the specified key, use the ceilingKey method */ //this will return 9, i.e. key >= 7 System.out.println( tmapNumbers.ceilingKey(7) ); //this will return 9, i.e. key >= 9 System.out.println( tmapNumbers.ceilingKey(9) ); //this will return null as there is no key which is > 10 System.out.println( tmapNumbers.ceilingKey(10) ); |
Output
1 2 3 |
7 9 null |
You can also use the higherEntry
and ceilingEntry
methods instead of the higherKey
and ceilingKey
methods respectively to get an entry instead of the key.
To get a value, get the entry and then use the getValue
method of the Map.Entry to get value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(3, "Three"); tmapNumbers.put(5, "Five"); tmapNumbers.put(7, "Seven"); tmapNumbers.put(9, "Nine"); //this will return entry having key >= 7 System.out.println( tmapNumbers.ceilingEntry(7) ); //to get value if(tmapNumbers.ceilingKey(7) != null ) System.out.println( tmapNumbers.ceilingEntry(7).getValue() ); |
Output
1 2 |
7=Seven Seven |
This example is a part of the TreeMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap