NavigableMap<String, Integer> descendingMap = map.descendingMap();
Set<Map.Entry<String, Integer>> entrySet = descendingMap.entrySet();
http://tutorials.jenkov.com/java-collections/navigablemap.html
NavigableSet reverse = map.descendingKeySet();
The
descendingMap()
method returns a NavigableMap
which is a view of the original Map
. The order of the elements in this view map is reverse of the order of the original map. Being a view of the original map, any changes to this view is also reflected in the original map.
Here is a simple example:
NavigableMap descending = map.descendingMap();
NavigableMap original = new TreeMap(); original.put("1", "1"); original.put("2", "2"); original.put("3", "3"); //this headmap1 will contain "1" and "2" SortedMap headmap1 = original.headMap("3"); //this headmap2 will contain "1", "2", and "3" because "inclusive"=true NavigableMap headmap2 = original.headMap("3", true);
The
tailMap()
method works the same way, except it returns all elements that are higher than the given parameter element.
The
subMap()
allows you to pass two parameters demarcating the boundaries of the view map to return. Here is an example:NavigableMap original = new TreeMap(); original.put("1", "1"); original.put("2", "2"); original.put("3", "3"); original.put("4", "4"); original.put("5", "5"); //this submap1 will contain "3", "3" SortedMap submap1 = original.subMap("2", "4"); //this submap2 will contain ("2", "2") ("3", "3") and ("4", "4") because // fromInclusive=true, and toInclusive=true NavigableMap submap2 = original.subMap("2", true, "4", true);
NavigableMap original = new TreeMap(); original.put("1", "1"); original.put("2", "2"); original.put("3", "3"); //ceilingKey will be "2". Object ceilingKey = original.ceilingKey("2"); //floorKey will be "2". Object floorKey = original.floorKey("2");
The
floorKey()
method does the opposite of ceilingKey()
The
higherKey()
method returns the least (smallest) element in this map that is greater than (not equal too) the element passed as parameter to the higherKey()
method. Here is an example:NavigableMap original = new TreeMap(); original.put("1", "1"); original.put("2", "2"); original.put("3", "3"); //higherKey will be "3". Object higherKey = original.higherKey("2"); //lowerKey will be "1" Object lowerKey = original.lowerKey("2");
NavigableMap original = new TreeMap(); original.put("1", "1"); original.put("2", "2"); original.put("3", "3"); //higherEntry will be ("3", "3"). Map.Entry higherEntry = original.higherEntry("2"); //lowerEntry will be ("1", "1") Map.Entry lowerEntry = original.lowerEntry("2");Java Collections - NavigableMap | tutorials.jenkov.com
descendingKeySet() and descendingMap()
The
descendingKeySet()
method returns a NavigableSet
in which the order of the elements is reversed compared to the original key set. The returned "view" is backed by the original NavigableSet
ket set, so changes to the descending set are also reflected in the original set. However, you should not remove elements directly from the key set. Use the Map.remove()
method instead.The
descendingMap()
method returns a NavigableMap
which is a view of the original Map
. The order of the elements in this view map is reverse of the order of the original map. Being a view of the original map, any changes to this view is also reflected in the original map.
The
headMap()
method returns a view of the original NavigableMap
which only contains elements that are "less than" the given element. Here is an example:
The
tailMap()
method works the same way, except it returns all elements that are higher than the given parameter element.
The
subMap()
allows you to pass two parameters demarcating the boundaries of the view map to return. Here is an example:The
ceilingKey()
method returns the least (smallest) key in this map that is greater than or equal to the element passed as parameter to the ceilingKey()
method.
The
floorKey()
method does the opposite of ceilingKey()
The
ceilingEntry(), floorEntry(), higherEntry() and lowerEntry()higherKey()
method returns the least (smallest) element in this map that is greater than (not equal too) the element passed as parameter to the higherKey()
method. Here is an example:pollFirstEntry() and pollLastEntry()
The
pollFirstEntry()
method returns and removes the "first" entry (key + value) in the NavigableMap
or null if the map is empty. The pollLastEntry()
returns and removes the "last" element in the map or null if the map is empty. "First" means smallest element according to the sort order of the keys. "Last" means largest key according to the element sorting order of the map.