AdSense

AdSense3

Monday 7 September 2015

Java HashMap class

  • A HashMap contains values based on the key. It implements the Map interface and extends AbstractMap class.
  • It contains only unique elements.
  • It may have one null key and multiple null values.
  • It maintains no order.

Hierarchy of HashMap class:

HashMap class hierarchy

Example of HashMap class:

  1. import java.util.*;  
  2. class TestCollection13{  
  3.  public static void main(String args[]){  
  4.    
  5.   HashMap<Integer,String> hm=new HashMap<Integer,String>();  
  6.   
  7.   hm.put(100,"Amit");  
  8.   hm.put(101,"Vijay");  
  9.   hm.put(102,"Rahul");  
  10.   
  11.   for(Map.Entry m:hm.entrySet()){  
  12.    System.out.println(m.getKey()+" "+m.getValue());  
  13.   }  
  14.  }  
  15. }  
Output:102 Rahul
       100 Amit
       101 Vijay

What is difference between HashSet and HashMap?

HashSet contains only values whereas HashMap contains entry(key and value).

No comments:

Post a Comment