NavigableMap key
In this chapter you will learn:
- How to get keys with greater value
- How to get key with value less than a give value
- How to get greater key value for a given key
- 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:
- Removes and returns a key-value mapping associated with the least key in this map
- Removes and returns a key-value mapping associated with the greatest key in this map
Home » Java Tutorial » Map