import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapStoreObject {
public static void main(String[] args) {
Map<Integer, Emp> tm = new TreeMap<Integer, Emp>();
tm.put(1, new Emp("yogesh", 100));
tm.put(2, new Emp("shivam", 200));
tm.put(3, new Emp("ankush",250));
tm.put(4, new Emp("anurag",300));
tm.put(5, new Emp("ravi", 370));
//Returns a Set view of the keys contained in this map.
Set<Integer> keys = tm.keySet();
for (Integer key : keys) {
Emp emp = tm.get(key);
System.out.println(" key ="+ key +" name ="+emp.getName()+" id = "+ emp.getName());
}
}
}
class Emp{
private String name;
private int id;
public Emp(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Output:
key =1 name =yogesh id = yogesh
key =2 name =shivam id = shivam
key =3 name =ankush id = ankush
key =4 name =anurag id = anurag
key =5 name =ravi id = ravi
import java.util.Set;
import java.util.TreeMap;
public class TreeMapStoreObject {
public static void main(String[] args) {
Map<Integer, Emp> tm = new TreeMap<Integer, Emp>();
tm.put(1, new Emp("yogesh", 100));
tm.put(2, new Emp("shivam", 200));
tm.put(3, new Emp("ankush",250));
tm.put(4, new Emp("anurag",300));
tm.put(5, new Emp("ravi", 370));
//Returns a Set view of the keys contained in this map.
Set<Integer> keys = tm.keySet();
for (Integer key : keys) {
Emp emp = tm.get(key);
System.out.println(" key ="+ key +" name ="+emp.getName()+" id = "+ emp.getName());
}
}
}
class Emp{
private String name;
private int id;
public Emp(String name, int id) {
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Output:
key =1 name =yogesh id = yogesh
key =2 name =shivam id = shivam
key =3 name =ankush id = ankush
key =4 name =anurag id = anurag
key =5 name =ravi id = ravi
No comments:
Post a Comment