Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java HashMap Example

  1. /*
  2. Java HashMap example.
  3. This Java HashMap example describes the basic operations performed on the HashMap.
  4. */
  5.  
  6. import java.util.HashMap;
  7. import java.util.Iterator;
  8.  
  9. public class HashMapExample{
  10.    
  11.     public static void main(String args[]){
  12.        
  13.         // constructs a new empty HashMap with default initial capacity        
  14.         HashMap hashMap = new HashMap();
  15.        
  16.         /*
  17.        
  18.         To specify initial capacity, use following constructor        
  19.         HashMap HashMap = new HashMap(100);
  20.        
  21.         To create HashMap from map use following constructor        
  22.         HashMap HashMap = new HashMap(Map myMap);
  23.        
  24.         */
  25.        
  26.         hashMap.put("One", new Integer(1)); // adding value into HashMap
  27.        
  28.         hashMap.put("Two", new Integer(2));
  29.        
  30.         hashMap.put("Three", new Integer(3));
  31.        
  32.         /*
  33.         IMPORTANT : We CAN NOT add primitives to the HashMap. We have to wrap it into
  34.         one of the wrapper classes before adding.
  35.         */
  36.        
  37.         /*
  38.        
  39.         To copy all key - value pairs from any Map to HashMap use putAll method.
  40.         Signature of putAll method is,        
  41.         void putAll(Map m)
  42.        
  43.         */
  44.        
  45.         //get number of keys present in the HashMap        
  46.         System.out.println("HashMap contains " + hashMap.size() + " key value pairs.");
  47.        
  48.         /*
  49.        
  50.         To check whether HashMap is empty or not, use isEmpty() method.        
  51.         isEmpty() returns true is HashMap is empty, otherwise false.
  52.        
  53.         /*
  54.        
  55.         Finding particular value from the HashMap:
  56.        
  57.         HashMap's containsValue method returns boolean depending upon
  58.         the presence of the value in given HashMap
  59.        
  60.         Signature of the containsValue method is,        
  61.         boolean containsValue(Object value)
  62.        
  63.         */
  64.        
  65.         if(hashMap.containsValue(new Integer(1))){
  66.             System.out.println("HashMap contains 1 as value");
  67.         }else{
  68.             System.out.println("HashMap does not contain 1 as value");
  69.         }
  70.        
  71.         /*
  72.        
  73.         Finding particular Key from the HashMap:        
  74.         HashMap's containsKey method returns boolean depending upon the
  75.         Presence of the key in given HashMap
  76.        
  77.         Signature of the method is,        
  78.         boolean containsKey(Object key)
  79.        
  80.         */
  81.        
  82.         if( hashMap.containsKey("One") ){
  83.             System.out.println("HashMap contains One as key");
  84.         }else{
  85.             System.out.println("HashMap does not contain One as value");
  86.         }
  87.        
  88.         /*
  89.         Use get method of HashMap to get value mapped to particular key.        
  90.         Signature of the get method is,        
  91.         Object get(Object key)        
  92.         */
  93.        
  94.         Integer one = (Integer) hashMap.get("One");        
  95.         System.out.println("Value mapped with key \"One\" is " + one);
  96.        
  97.         /*
  98.         IMPORTANT:  get method returns Object, so we need to downcast it.
  99.         */
  100.        
  101.         /*
  102.         To get all keys stored in HashMap use keySet method        
  103.         Signature of the keysSet method is,        
  104.         Set keySet()
  105.         */
  106.        
  107.         System.out.println("Retrieving all keys from the HashMap");        
  108.         Iterator iterator = hashMap.keySet().iterator();
  109.        
  110.         while(iterator. hasNext()){        
  111.             System.out.println(iterator.next());
  112.         }
  113.        
  114.         /*
  115.         To get all values stored in HashMap use entrySet() method.
  116.         Signature of the entrySet() method is,        
  117.         Set entrySet()
  118.         */
  119.        
  120.         System.out.println("Retrieving all values from the HashMap");
  121.         iterator = hashMap.entrySet().iterator();
  122.        
  123.         while(iterator. hasNext()){        
  124.             System.out.println(iterator.next());
  125.         }
  126.        
  127.         /*
  128.         To remove particular key - value pair from the HashMap use remove method.        
  129.         Signature of remove method is,        
  130.         Object remove(Object key)        
  131.         This method returns value that was mapped to the given key,
  132.         otherwise null if mapping not found.
  133.        
  134.         */
  135.        
  136.         System.out.println( hashMap.remove("One") + " is removed from the HashMap.");
  137.        
  138.     }
  139.  
  140. }
  141.  
  142. /*
  143. OUTPUT of the above given Java HashMap Example would be :
  144. HashMap contains 3 key value pair.
  145. HashMap contains 1 as value
  146. HashMap contains One as key
  147. Value mapped with key "One" is 1
  148. Retrieving all keys from the HashMap
  149. Three
  150. Two
  151. One
  152. Retrieving all values from the HashMap
  153. Three=3
  154. Two=2
  155. One=1
  156. 1 is removed from the HashMap.
  157. */
Your rating: None Average: 4.4 (70 votes)
Share this