Core Java Tutorial

Core Java Tutorial

Java HashMap Example Program

Java HashMap:

Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

package com.candidjava.core;
import java.util.HashMap;
 
 
public class HashMapExample {
 
  
  public static void main(String[] args) {
    HashMap<String,String> hm=new HashMap<String,String>();
    hm.put("name","candid");
    hm.put("email","[email protected]");
    hm.put("password", "123456");
    hm.put("street", "shanthi nagar");
    hm.put("password", "789");
    hm.put(null, "chennai");
    hm.put(null, "Delhi");
    hm.put("altemail", null);
    hm.put("altph", null);
    
    
    System.out.println("HashMap ..  "+hm);
    System.out.println("size ... "+hm.size());		
    
  }
  
}
Output:
HashMap ..  {null=Delhi, password=789, altemail=null, street=shanthi nagar, name=candid, altph=null, [email protected]}
size ... 7