This example shows how NavigableMap in Java works.
A program for TreeMap implementation of NavigableMap interface with SortedMap.
— A NavigableMap is nothing but a SortedMap extended with navigation methods returning the closest matches for given search targets.
— A NavigableMap may be accessed and traversed in either ascending or descending key order.
— Submaps of any NavigableMap must implement the NavigableMap interface.
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 26 27 28 29 30 31 32 33 |
import java.util.NavigableMap; import java.util.TreeMap; public class NavigableMapDemo { public static void main(String[] args) { NavigableMap <string, integer=""> navigableMap = new TreeMap <string, integer=""> (); navigableMap.put("X", 500); navigableMap.put("B", 600); navigableMap.put("A", 700); navigableMap.put("T", 800); navigableMap.put("Y", 900); navigableMap.put("Z", 200); System.out.printf("Descending Set : %s%n", navigableMap.descendingKeySet()); System.out.printf("Floor Entry : %s%n", navigableMap.floorEntry("L")); System.out.printf("First Entry : %s%n", navigableMap.firstEntry()); System.out.printf("Last Key : %s%n", navigableMap.lastKey()); System.out.printf("First Key : %s%n", navigableMap.firstKey()); System.out.printf("Original Map : %s%n", navigableMap); System.out.printf("Reverse Map : %s%n", navigableMap.descendingMap()); } }</string,></string,> |
Output:
Descending Set : [Z, Y, X, T, B, A]
Floor Entry : B=600
First Entry : A=700
Last Key : Z
First Key : A
Original Map : {A=700, B=600, T=800, X=500, Y=900, Z=200}
Reverse Map : {Z=200, Y=900, X=500, T=800, B=600, A=700}
Floor Entry : B=600
First Entry : A=700
Last Key : Z
First Key : A
Original Map : {A=700, B=600, T=800, X=500, Y=900, Z=200}
Reverse Map : {Z=200, Y=900, X=500, T=800, B=600, A=700}