Friday, May 13, 2016

HashMap in java

package com.home;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapDemo1 {
   
public static void main(String[] args) {
Map<Integer, String> a = new HashMap<Integer, String>();

a.put(1, "hari");
a.put(2, "Gopal");
a.put(3, "RAm");
a.put(4, "Mohan");
a.put(5, "Daro");

int size = a.size();

System.out.println(size);


a.remove(1);
System.out.println(a);

Set set = a.entrySet();

Iterator it = set.iterator();

while(it.hasNext()){

Map.Entry me = (Map.Entry) it.next();

System.out.println("This is key = "+me.getKey()+" : "+"This is value "+me.getValue());
}
}
}
The Output of the Following Program is:

5
{2=Gopal, 3=RAm, 4=Mohan, 5=Daro}
This is key = 2 : This is value Gopal
This is key = 3 : This is value RAm
This is key = 4 : This is value Mohan
This is key = 5 : This is value Daro

0 comments:

Post a Comment