Monday, 23 April 2018

//Map example..........................

package collectctiondemo;

public class EmployeeData {

int empid;
String empname;
String empadd;
String empemail;
public int getEmpid() {
return empid;
}
public void setEmpid(int empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
public String getEmpadd() {
return empadd;
}
public void setEmpadd(String empadd) {
this.empadd = empadd;
}
public String getEmpemail() {
return empemail;
}
public void setEmpemail(String empemail) {
this.empemail = empemail;
}


}

==============================================================================

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 MapEmployeeData {

public static void main(String[] args) {
Map<String, EmployeeData> m=new HashMap<String,EmployeeData>();
EmployeeData e1=new EmployeeData();
e1.setEmpid(101);
e1.setEmpname("govind");
e1.setEmpadd("madhubani");
e1.setEmpemail("govindkhan@gmail.com");
m.put("g", e1);

EmployeeData e2=new EmployeeData();
e2.setEmpid(102);
e2.setEmpname("murari");
e2.setEmpadd("sakri");
e2.setEmpemail("murari@gmail.com");
m.put("m", e2);

EmployeeData e3=new EmployeeData();
e3.setEmpid(103);
e3.setEmpname("satyam");
e3.setEmpadd("hanumannagar");
e3.setEmpemail("satyam@gmail.com");
m.put("s", e3);


EmployeeData e4=new EmployeeData();
e4.setEmpid(104);
e4.setEmpname("rishabh");
e4.setEmpadd("madhubani");
e4.setEmpemail("rishabh@gmail.com");
m.put("r", e4);

EmployeeData e5=new EmployeeData();
e5.setEmpid(105);
e5.setEmpname("shivam");
e5.setEmpadd("jhanjharpur");
e5.setEmpemail("shivam@gmail.com");
m.put("sh", e4);

EmployeeData e6=new EmployeeData();
e6.setEmpid(106);
e6.setEmpname("murlidhar");
e6.setEmpadd("madhubani");
e6.setEmpemail("murlidhar@gmail.com");
m.put("mu", e6);

Set<Entry<String, EmployeeData>> s = m.entrySet();
Iterator<Entry<String, EmployeeData>> it = s.iterator();
while (it.hasNext()) {
Entry<String, EmployeeData> kk = it.next();
String s1=kk.getKey();
System.out.println(s1);
EmployeeData vv = kk.getValue();
System.out.println(vv.getEmpid()+"----"+vv.getEmpname()+"----"+vv.getEmpadd()+"----"+vv.getEmpemail());


}
}

}
===============================================================================

o/p:--------------------
r
104----rishabh----madhubani----rishabh@gmail.com
s
103----satyam----hanumannagar----satyam@gmail.com
sh
104----rishabh----madhubani----rishabh@gmail.com
g
101----govind----madhubani----govindkhan@gmail.com
mu
106----murlidhar----madhubani----murlidhar@gmail.com
m
102----murari----sakri----murari@gmail.com

No comments:

Post a Comment