ConcurrentSkipListMap - Iterate on keys by obtaining keySet, Iterate on values by obtaining values, Iterate on entry by obtaining entrySet





ConcurrentSkipListMap - Iterate on keys by obtaining keySet, Iterate on values by obtaining values, Iterate on entry by obtaining entrySet.


import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentSkipListMap;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ConcurrentSkipListMapTest {
   public static void main(String args[]){
          Map<Integer,String> concurrentSkipListMap=new ConcurrentSkipListMap<Integer,String>();
          concurrentSkipListMap.put(11, "ankit");
          concurrentSkipListMap.put(21, "javaMadeSoEasy");
      System.out.println("\n---1. Iterate on keys, by obtaining iterator on keySet---");
          Iterator<Integer> keyIterator=concurrentSkipListMap.keySet().iterator();
          while(keyIterator.hasNext()){
                 System.out.println(keyIterator.next());
          }
          System.out.println("\n---obtain keySet from Map---");
          Set<Integer> keySet=concurrentSkipListMap.keySet();
          System.out.println("---now iterate on keySet using enhanced for loop---");
          for(Integer key :keySet){
                 System.out.println(key);
          }
         
      System.out.println("\n---2. Iterate on values, by obtaining iterator on values---");
          Iterator<String> valueIterator=concurrentSkipListMap.values().iterator();
          while(valueIterator.hasNext()){
                 System.out.println(valueIterator.next());
          }
          System.out.println("\n---obtain values from Map---");
          Collection<String> collection=concurrentSkipListMap.values();
          System.out.println("---now iterate on values using enhanced for loop---");
          for(String value :collection){
                 System.out.println(value);
          }
      System.out.println("\n---3. Iterate on entry, by obtaining iterator on entrySet---");
          Iterator<Entry<Integer, String>> entryIterator=concurrentSkipListMap.entrySet().iterator();
          while(entryIterator.hasNext()){
                 System.out.println(entryIterator.next());
          }
          System.out.println("\n---obtain entrySet from Map---");
          Set<Entry<Integer, String>> entrySet=concurrentSkipListMap.entrySet();
          System.out.println("---now iterate on entrySet using enhanced for loop---");
          for(Entry<Integer, String> entry:entrySet){
                 System.out.println(entry);
          }
   }
}
/*OUTPUT
---1. Iterate on keys, by obtaining iterator on keySet---
11
21
---obtain keySet from Map---
---now iterate on keySet using enhanced for loop---
11
21
---2. Iterate on values, by obtaining iterator on values---
ankit
javaMadeSoEasy
---obtain values from Map---
---now iterate on values using enhanced for loop---
ankit
javaMadeSoEasy
---3. Iterate on entry, by obtaining iterator on entrySet---
11=ankit
21=javaMadeSoEasy
---obtain entrySet from Map---
---now iterate on entrySet using enhanced for loop---
11=ankit
21=javaMadeSoEasy
*/



RELATED LINKS>

Program to create method that provides functionality similar to putIfAbsent method of ConcurrentHashMap and to be used with HashMap


Program to use ConcurrentHashMap’s putIfAbsent method


No comments:

Post a Comment