In this example we will discuss how can descendingKeySet() method be implemented in ConcurrentNavigableMap interface in java.
Java descendingKeySet() method of ConcurrentNavigableMap interface.
In this example we will discuss how can descendingKeySet() method be implemented in ConcurrentNavigableMap interface in java.
Syntax
NavigableSet<K> descendingKeySet()
This method gives back the NavigableSet view of keys in inverse order. Since map returned the set back so, any changes occur into the map are reflected into the set, and vice-versa. Element removal is supported by the set that deletes the mapping from the underlying map using Iterator.remove, Set.remove, removeAll(), retainAll(), and clear() methods. add() or addAll() method is not supported by it.
Parameter description
This method has no parameter.
Example of descendingKeySet() method
In this example we will show you how does descendingKeySet() method work in ConcurrentNavigableMap interface. This example will help you to understand how can you obtain a set of key in reverse order from the underlying map.
Exmaple :
package devmanuals.com; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentNavigableMap; import java.util.NavigableSet; public class CNMDescendingKeySet { public static void main (String args[]){ ConcurrentNavigableMapcnm = new ConcurrentSkipListMap (); cnm.put(1,"A"); cnm.put(2,"B"); cnm.put(3,"C"); cnm.put(4,"D"); cnm.put(5,"E"); cnm.put(6,"F"); System.out.println("Mappings = "+cnm); NavigableSet s= cnm.keySet(); System.out.println("Set of keys = "+s); s=cnm.descendingKeySet(); System.out.println("Set of keys in descending order = "+s); System.out.println("Size of map = "+cnm.size()); for(int i=0;i<=6;i++){ cnm.remove(6); } System.out.println("Remainig set of keys = "+s); System.out.println("Remaining size of map = "+cnm.size()); } }
Output :
Mappings = {1=A, 2=B, 3=C, 4=D, 5=E, 6=F} Set of keys = [1, 2, 3, 4, 5, 6] Set of keys in descending order = [6, 5, 4, 3, 2, 1] Size of map = 6 Remainig set of keys = [5, 4, 3, 2, 1] Remaining size of map = 5 |
[ 0 ] Comments