Monday, 23 April 2018


//Map Demonstration .................................
package collectctiondemo;

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

public class MapDemo {

public static void main(String[] args) {
Map<String, String> m=new HashMap<>();
m.put("g", "govind");
m.put("b", "ballabh");
m.put("k", "khan");
m.put("m", "madhubani");
m.put("h", "hanumannagar");
m.put("b", "bihar");//replace old value ballabh

System.out.println("---------map contains data in the form of key value-----------");
System.out.println(m);
System.out.println("----------Total Key--------------");
System.out.println(m.keySet());
System.out.println("-----------Total value-------------");
System.out.println(m.values());
System.out.println("----------How to iterate map------------");
Set<Entry<String, String>> s = m.entrySet();
Iterator<Entry<String, String>> it = s.iterator();
while (it.hasNext()) {
Entry<String, String> s1 = it.next();
String kk = s1.getKey();
System.out.println(kk);
String vv = s1.getValue();
System.out.println(vv);

}



}
}


o/p:-------------

---------map contains data in the form of key value-----------
{b=bihar, g=govind, h=hanumannagar, k=khan, m=madhubani}
----------Total Key--------------
[b, g, h, k, m]
-----------Total value-------------
[bihar, govind, hanumannagar, khan, madhubani]
----------How to iterate map------------
b
bihar
g
govind
h
hanumannagar
k
khan
m
madhubani

No comments:

Post a Comment