NavigableMap key

In this chapter you will learn:

  1. How to get keys with greater value
  2. How to get key with value less than a give value
  3. How to get greater key value for a given key
  4. How to get greatest key value for a given key

Key with greater value

K ceilingKey(K key) Returns the least key greater than or equal to the given key.

import java.util.NavigableMap;
import java.util.TreeMap;
/*from  j  a v a2 s  .co m*/
public class Main {
  public static void main(String args[]) {
    
    NavigableMap<String, String> nav = new TreeMap<String, String>();
    nav.put("A", "a");
    nav.put("B", "b");
    nav.put("C", "c");
    nav.put("D", "d");
    nav.put("E", "e");
    nav.put("F", "f");
    System.out.println(nav);
    System.out.println(nav.ceilingKey("B"));
  }
}

The code above generates the following result.

Floor key values

K floorKey(K key) Returns the greatest key less than or equal to the given key.

import java.util.NavigableMap;
import java.util.TreeMap;
/*from j a  v a  2s.c o  m*/
public class Main {
  public static void main(String args[]) {
    
    NavigableMap<String, String> nav = new TreeMap<String, String>();
    nav.put("A", "a");
    nav.put("B", "b");
    nav.put("C", "c");
    nav.put("D", "d");
    nav.put("E", "e");
    nav.put("F", "f");
    System.out.println(nav);
    System.out.println(nav.floorKey("G"));
  }
}

The code above generates the following result.

Higher key

K higherKey(K key) Returns the least key strictly greater than the given key.

import java.util.NavigableMap;
import java.util.TreeMap;
//from j a  v a  2 s . c o m
public class Main {
  public static void main(String args[]) {
    
    NavigableMap<String, String> nav = new TreeMap<String, String>();
    nav.put("A", "a");
    nav.put("B", "b");
    nav.put("C", "c");
    nav.put("D", "d");
    nav.put("E", "e");
    nav.put("F", "f");
    System.out.println(nav);
    System.out.println(nav.higherKey("G"));
  }
}

The code above generates the following result.

Lower key

K lowerKey(K key) Returns the greatest key strictly less than the given key, or null if there is no such key.

import java.util.NavigableMap;
import java.util.TreeMap;
/*from   j a va 2  s.  c o  m*/
public class Main {
  public static void main(String args[]) {
    
    NavigableMap<String, String> nav = new TreeMap<String, String>();
    nav.put("A", "a");
    nav.put("B", "b");
    nav.put("C", "c");
    nav.put("D", "d");
    nav.put("E", "e");
    nav.put("F", "f");
    System.out.println(nav);
    System.out.println(nav.lowerKey("G"));
  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Removes and returns a key-value mapping associated with the least key in this map
  2. Removes and returns a key-value mapping associated with the greatest key in this map
Home » Java Tutorial » Map
Map interface
Map element adding
Map.Entry class
Map key
Map value
Map key/value search
Map delete/remove
Map comparison
HashMap Class
HashMap search
HashMap clone
TreeMap
TreeMap key
TreeMap head sub map
TreeMap tail sub map
TreeMap sub map
NavigableMap
NavigableMap key
NavigableMap key-value pair
LinkedHashMap Class
IdentityHashMap
SortedMap