ConcurrentHashMap program to Iterate on keys and values in java


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




HashMap and ConcurrentHashMap - Similarity and Differences


HashMap vs Hashtable vs LinkedHashMap vs TreeMap - Differences


Map hierarchy in java - Detailed - HashMap, Hashtable, ConcurrentHashMap, LinkedHashMap, TreeMap, ConcurrentSkipListMap, IdentityHashMap, WeakHashMap, EnumMap classes



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.ConcurrentHashMap;
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class ConcurrentHashMapIteratorExample {
public static void main(String args[]){
  
   Map<Integer,String> concurrentHashMap=new ConcurrentHashMap<Integer,String>();
   //maintains natural order of keys
   concurrentHashMap.put(21, "javaMadeSoEasy");
   concurrentHashMap.put(11, "ankit");
  
   System.out.println("\n---1. Iterate on keys, by obtaining iterator on keySet---");
   Iterator<Integer> keyIterator=concurrentHashMap.keySet().iterator();
   while(keyIterator.hasNext()){
          System.out.println(keyIterator.next());
   }
   System.out.println("\n---obtain keySet from Map---");
   Set<Integer> keySet=concurrentHashMap.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=concurrentHashMap.values().iterator();  
   while(valueIterator.hasNext()){
          System.out.println(valueIterator.next());
   }
   System.out.println("\n---obtain values from Map---");
   Collection<String> collection=concurrentHashMap.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=concurrentHashMap.entrySet().iterator();
   while(entryIterator.hasNext()){
          System.out.println(entryIterator.next());
   }
   System.out.println("\n---obtain entrySet from Map---");
   Set<Entry<Integer, String>> entrySet=concurrentHashMap.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---
21
11
---obtain keySet from Map---
---now iterate on keySet using enhanced for loop---
21
11
---2. Iterate on values, by obtaining iterator on values---
javaMadeSoEasy
ankit
---obtain values from Map---
---now iterate on values using enhanced for loop---
javaMadeSoEasy
ankit
---3. Iterate on entry, by obtaining iterator on entrySet---
21=javaMadeSoEasy
11=ankit
---obtain entrySet from Map---
---now iterate on entrySet using enhanced for loop---
21=javaMadeSoEasy
11=ankit
*/




RELATED LINKS>


HashMap - Iterator on keySet, values and entrySet is fail-safe or fail-fast?


HashMap - synchronizing map using Collections.synchronizedMap




ArrayList - add, add element at specific index methods program





No comments:

Post a Comment