I am facing a problem to iterate and show the values using LinkedHashMap. I have a Model class which have the getter setter for attributes.
This is my main class...
public class main {
public static void main(String[] args) throws SQLException {
DemoHandler handler = new DemoHandler();
LinkedHashMap<String, Model> hashMap = new LinkedHashMap<String, Model>();
Model modelInfo = new Model();
modelInfo.setId(1);
modelInfo.setName("Subho");
modelInfo.setEmail("sm@gammainfotech.com");
modelInfo.setAge("24");
modelInfo.setGender("Male");
hashMap.put("100", modelInfo);
System.out.println(modelInfo.getName());// It shows Subho, which is fine
modelInfo.setId(2);
modelInfo.setName("Diba");
modelInfo.setEmail("sm@gammainfotech.com");
modelInfo.setAge("25");
modelInfo.setGender("Male");
hashMap.put("101", modelInfo);
System.out.println(modelInfo.getName());// It shows Diba, which is fine
modelInfo.setId(3);
modelInfo.setName("Jeet");
modelInfo.setEmail("sm@gammainfotech.com");
modelInfo.setAge("28");
modelInfo.setGender("Male");
hashMap.put("102", modelInfo);
System.out.println(modelInfo.getName());// It shows Jeet, which is fine
for (Map.Entry<String, Model> entry : hashMap.entrySet()) {
Model m = entry.getValue();
System.out.println(m.getName());// Here I can see only Jeet thrice. The iterator iterates 3 times which is fine but the value it gives only the last data I entry. It should shows Subho,Diba,Jeet here.
}
}
}
Now when I run this the out put shows...
Subho Diba Jeet
Jeet Jeet Jeet
Please help me to show all the values..