Java Program to Get TreeMap Key, Value, or Entry Greater or Less than the Specified Value


A TreeMap is a class provided by Java that implements Map Interface. It is a collection of key-value pairs and the key-value pairs in TreeMap are stored in sorted Order based on key values. An Entry is defined as a key-value pair in a TreeMap. In this article, we will look in detail how to get TreeMap key-value pair or entry greater or less than the specified value.

Now, we will be looking into some of the inbuilt methods and their functionality of TreeMap which are used in this particular article.

Methods Used

higherKey() − This method is used to return the least key greater than the given key if it is present , or else it will return null.

mapObject.higherKey(Object key)

higherEntry() − This method returns a key-value pair whose least key is strictly greater than the given key if present , or else it will return null.

mapObject.higherEntry(Object key)

lowerKey() − This method is used to return the greatest key strictly less than the given key, or else it will return null.

mapObject.lowerKey(Object key) 

lowerEntry() − This method returns a key-value pair whose greatest key is strictly less than the given key, or else it will return null.

mapObject.lowerEntry(Object key)

ceilingEntry() − This method returns a key-value pair whose least key is greater than or equal to the given key, or else it will return null.

mapObject.ceilingEntry(Object key)

floorEntry() − This method returns a key-value pair whose greater key is less than or equal to the given key, or else it will return null.

mapObject.floorEntry(Object key)

get( − This method is used to get the value associated with key, or else it will return null.

mapObject.get(Object key)

put () − This method is used to add a key-value pair to a TreeMap.It takes to parameters a key and a value.

mapObject.get(Object key, Object value)

Now, we discuss in detail using different methods in java to find the Tree Map Key,Value or Entry Greater or Less than the Specified Value using different examples.

Approach 1: Using higherKey(), lowerKey(), higherEntry(), and lowerEntry()

  • Create a TreeMap object and add key,values using put() method.

  • Print least key,value,Entry greater than given key using higherKey() and higherEntry().

  • Print greatest key,value,Entry less than given key using lowerKey() and lowerEntry().

Example

In this example, we will use the different inbuilt methods higherKey(), lowerKey(), higherEntry(), lowerEntry() provided by java to get the Key,Value, or Entry Greater or Less than the Specified value.

import java.util.*;
public class Main {
   public static void main(String[] args) {
      TreeMap<Integer, String> treeMap = new TreeMap<>();
      treeMap.put(1, "A");
      treeMap.put(2, "B");
      treeMap.put(3, "C");
      treeMap.put(4, "D");
      System.out.println("Key greater than 3: " + treeMap.higherKey(3));
      System.out.println("Value greater than 3: " + treeMap.get(treeMap.higherKey(3)));
      System.out.println("Entry greater than 3: " + treeMap.higherEntry(3));
      System.out.println("Key less than 3: " + treeMap.lowerKey(3));
      System.out.println("Value less than 3: " + treeMap.get(treeMap.lowerKey(3)));
      System.out.println("Entry less than 3: " + treeMap.lowerEntry(3));
   }
}

Output

Key greater than 3: 4
Value greater than 3: D
Entry greater than 3: 4=D
Key less than 3: 2
Value less than 3: B
Entry less than 3: 2=B

Approach 2: Using ceilingEntry() and floorEntry()

  • Create a TreeMap object and add key, values using the put() method.

  • Print least key,value,Entry greater than given key using higherKey() and ceilingEntry()

  • Print greatest key,value,Entry less than given key using lowerKey() and floorEntry()

Example

In this example, we will use the different inbuilt methods ceilingEntry() and floorEntry() provided by java to get the Key, Value, or Entry Greater or Less than the Specified value.

import java.util.*;
public class Main {
   public static void main(String[] args) {
      TreeMap<Integer, String> treeMap = new TreeMap<>();
      treeMap.put(1, "A");
      treeMap.put(3, "C");
      treeMap.put(4, "D");
      System.out.println("Key greater than 3: " + treeMap.higherKey(3));
      System.out.println("Value greater than 3: " + treeMap.get(treeMap.higherKey(3)));
      System.out.println("Key less than 3: " + treeMap.lowerKey(3));
      System.out.println("Value less than 3: " + treeMap.get(treeMap.lowerKey(3)));
      System.out.println("Entry greater than  2: " + treeMap.ceilingEntry(2));
      System.out.println("Entry less than  2: " + treeMap.floorEntry(2));
   }
}

Output

Key greater than 3: 4
Value greater than 3: D
Key less than 3: 1
Value less than 3: A
Entry greater than  2: 3=C
Entry less than  2: 1=A

Thus, in this article we discussed different approaches to find the TreeMap key, Value, or Entry Greater or Less than the Specified Value.

Updated on: 16-Aug-2023

154 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements