对map使用Iterator遍历时可以修改value的值(即引用传递)

    public static void main(String[] args) {
Map map = new HashMap();
Man person1 = new Man("name1", "age1"); 
Man person2 = new Man("name2", "age2"); 
Man person3 = new Man("name3", "age3"); 
Man person4 = new Man("name4", "age4"); 
map.put(person1.getName(), person1);
map.put(person2.getName(), person2);
map.put(person3.getName(), person3);
map.put(person4.getName(), person4);
Iterator> iterator = map.entrySet().iterator();
while(iterator.hasNext()) {
Entry next = iterator.next();
Man person = next.getValue();
person.setSay(person.getName()+person.getAge());
}
for (Map.Entry entry : map.entrySet()) {
System.out.println(entry.getValue().getSay());
}

}

public class Man{
private String name;
private String age;

private String say;

        getter and setter.........

}

结果

对map使用Iterator遍历时可以修改value的值(即引用传递)_第1张图片

你可能感兴趣的:(对map使用Iterator遍历时可以修改value的值(即引用传递))