12 February 2013

How HashMap data structure works in java?

 import java.util.HashMap;  
 import java.util.Iterator;  
 import java.util.Map;  
 import java.util.Set;  
 public class Collections10 {  
 public static void main(String[] args) {  
 // Map declaration using generics  
 Map<String,String> map = new HashMap<String,String>();  
 // using of put method to insert key,value pairs  
 map.put("madhu", "Sri");  
 map.put("prasad", "bhagya");  
 map.put("laxman", "chandra");  
 // check if particular key exists in Hashmap using containsKey method  
 boolean x1 = map.containsKey("madhu");  
 System.out.println("key exist in map:"+x1);  
 // check if particular key exists in Hashmap using containsKey method  
 boolean x2 = map.containsValue("Sri");  
 System.out.println("value exist in map:"+x2);  
 // using of iterator to read each entry of Hashmap  
 for (Iterator it = map.entrySet().iterator(); it.hasNext();) {  
 Map.Entry entry = (Map.Entry) it.next();  
 Object key = entry.getKey();  
 Object value = entry.getValue();  
 System.out.println("using of iterator to read each entry of Hashmap:");  
 System.out.println("key :"+(String)key);  
 System.out.println("value :"+(String)value);  
 }  
 // using of iterator over keys  
 for (Iterator it1 = map.keySet().iterator(); it1.hasNext();) {  
 Object key1 = it1.next();  
 System.out.println("using of iterator over keys:");  
 System.out.println("key :"+(String)key1);  
 }  
 // using iterator over values  
 for (Iterator it2 = map.values().iterator(); it2.hasNext();) {  
 Object value2 = it2.next();  
 System.out.println("using of iterator over values:");  
 System.out.println("key :"+(String)value2);  
 }  
 // using set interface to read keySet  
 Set st = map.keySet();  
 Iterator it3 = st.iterator();  
 while (it3.hasNext())  
 System.out.println(it3.next());  
 // remove madhu from Set  
 st.remove("madhu");  
 System.out.println("madhu removed from set but is that still exist in map?");  
 System.out.println(map.containsKey("madhu"));  
 // checking the size of HashMap  
 System.out.println("size of Hashmap:"+map.size());  
 // performing remove operation on element of HashMap  
 System.out.println("removing madhu key from map:"+map.remove("madhu"));  
 //using get method  
 System.out.println("using get method on key prasad on map:"+map.get("prasad"));  
 //removal of all elements from map  
 map.clear();  
 }  
 }  


Output:
key exist in map:true
value exist in map:true
using of iterator to read each entry of Hashmap:
key :madhu
value :Sri
using of iterator to read each entry of Hashmap:
key :laxman
value :chandra
using of iterator to read each entry of Hashmap:
key :prasad
value :bhagya
using of iterator over keys:
key :madhu
using of iterator over keys:
key :laxman
using of iterator over keys:
key :prasad
using of iterator over values:
key :Sri
using of iterator over values:
key :chandra
using of iterator over values:
key :bhagya
madhu
laxman
prasad
madhu removed from set but is that still exist in map?
false
size of Hashmap:2
removing madhu key from map:null
using get method on key prasad on map:bhagya

References:
You can find internal implementation of HashMap in below link:

No comments:

Post a Comment