Sunday 14 June 2015

How to create HashMap In Java

import java.util.HashMap;
import java.util.Map;

public class CreateHashMap {

    public static void main(String[] args) {
       
    Map<Integer,String> map = new HashMap<Integer,String>();
       
    /*
     * Associates the specified value with the specified key in
       this map (optional operation). If the map previously
       contained a mapping for the key, the old value is
       replaced by the specified value
     */
        map.put(1,"ankush");
        map.put(2, "amit");
        map.put(3,"shivam");
        map.put(4,"ankit");
        map.put(5, "yogesh");
       
        //print hashmap
        System.out.println("HashMap = "+map);
       
       
    }
   
}




Output:


HashMap = {1=ankush, 2=amit, 3=shivam, 4=ankit, 5=yogesh}

No comments:

Post a Comment