NavigableMap
Navigable map is a sorted map that can be iterated over in descending order as well as ascending order. Navigable maps are described by the NavigableMap interface, whose generic type is NavigableMap<K,V>, which extends SortedMap.
TreeMap is an example of a navigable map.
import java.util.Iterator;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
NavigableMap<String, Integer> navigableMap = new TreeMap<String, Integer>();
String[] letters = { "a", "b", "c" };
int[] ints = { 3, 2, 1 };
for (int i = 0; i < letters.length; i++){
navigableMap.put(letters[i], ints[i]);
}
System.out.println("Map = " + navigableMap);
NavigableSet<String> ns = navigableMap.navigableKeySet();
Iterator iter = ns.iterator();
while (iter.hasNext()){
System.out.print(iter.next() + " ");
}
System.out.println();
ns = navigableMap.descendingKeySet();
iter = ns.iterator();
while (iter.hasNext()){
System.out.print(iter.next() + " ");
}
System.out.println();
System.out.println("First entry = " + navigableMap.firstEntry());
System.out.println("Last entry = " + navigableMap.lastEntry());
System.out.println("Entry < a is " + navigableMap.lowerEntry("a"));
System.out.println("Entry > c is " + navigableMap.higherEntry("c"));
System.out.println("Poll first entry: " + navigableMap.pollFirstEntry());
System.out.println("Map = " + navigableMap);
System.out.println("Poll last entry: " + navigableMap.pollLastEntry());
System.out.println("Map = " + navigableMap);
}
}