我们在将自定义类型的值put进HashMap时,想要实现新值覆盖旧值,但实际上新值并不会覆盖旧值。代码如下:
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class Person{
private String id;
public Person(String id) {
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
'}';
}
}
public class TestDemo10 {
public static void main(String[] args) {
HashMap<Person, String> map = new HashMap<>();
map.put(new Person("001"), "fdjh");
map.put(new Person("002"),