In this example we will discuss how can keySet() method be implemented in ConcurrentNavigableMap interface in java. NavigableSet
Java keySet() method of ConcurrentNavigableMap interface.
In this example we will discuss how can keySet() method be implemented in ConcurrentNavigableMap interface in java.
Syntax
NavigableSet<K> keySet()
It returns a view of set of keys of NavigableSet contained by the underlying map. 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 keySet() method
In this example we will show you how does keySet() method work in ConcurrentNavigableMap interface. This example will help you to understand how can you find a set of keys from the underlying map.
Example :
package devmanuals.com; import java.util.concurrent.ConcurrentNavigableMap; import java.util.concurrent.ConcurrentSkipListMap; import java.util.NavigableSet; public class CNMKeySet { public static void main (String args[]){ ConcurrentNavigableMap<Integer,String> cnm = new ConcurrentSkipListMap<Integer,String>(); cnm.put(1,"A"); cnm.put(2,"B"); cnm.put(3,"C"); cnm.put(4,"D"); System.out.println("Mappings = "+cnm); System.out.println("Size of mappings = "+cnm.size()); NavigableSet<Integer> ns = cnm.keySet(); System.out.println("Set of keys = "+ns); System.out.println("First entry of the map = "+cnm.firstEntry()); System.out.println("Last entry of the map = "+cnm.lastEntry()); } }
Output :
Mappings = {1=A, 2=B, 3=C, 4=D} Size of mappings = 4 Set of keys = [1, 2, 3, 4] First entry of the map = 1=A Last entry of the map = 4=D |
[ 0 ] Comments