【发布时间】:2015-05-18 10:12:40
【问题描述】:
我在使用 LinkedHashMap 迭代和显示值时遇到问题。 我有一个 Model 类,它具有属性的 getter setter。
这是我的主要课程...
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.
}
}
}
现在当我运行它时,输出显示...
苏波 迪巴 吉特
杰特 吉特 吉特
请帮我显示所有值..
【问题讨论】:
-
在每个
modelInfo.setId()之前致电modelInfo = new Model();
标签: java dictionary linkedhashmap