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}

Leave a Reply

Your email address will not be published. Required fields are marked *

− 1 = 2