0

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..

Subho
  • 921
  • 5
  • 25
  • 48

2 Answers2

2

You are adding the same Model instance multiple times to the Map, so each time you are overwriting the properties of the previously added Model with the properties of the newly added Model.

You should add unique Model instances to the Map:

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 = new Model();
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 = new Model();
modelInfo.setId(3);
modelInfo.setName("Jeet");
modelInfo.setEmail("sm@gammainfotech.com");
modelInfo.setAge("28");
modelInfo.setGender("Male");
hashMap.put("102", modelInfo);
Eran
  • 387,369
  • 54
  • 702
  • 768
1

You solved your problem thanks to Eran's answer.

Now, I suggest you use an elegant new way to output your LinkedHashMap using java 8 features (Lambdas...)

This is the old way:

for (Map.Entry<String, Model> entry : hashMap.entrySet()) {
           Model m = entry.getValue();                
           System.out.println(m.getName());
}

This is the new way

hashMap.forEach((s, m) -> System.out.println(m.getName()));
MChaker
  • 2,610
  • 2
  • 22
  • 38