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