import java.util.HashMap; import java.util.Map; publicclass HashMapOver {publicstaticvoid main(String[] args) { Mapmap =new HashMap(); Person p=new Person("zhangsan", 12); System.out.println(map.size()); map.put(p,1); System.out.println(p.hashCode()); p.setName("lisi");// 因为p.name参与了hash值的计算,修改了之后hash值发生了变化,所以下面删除不掉 map.remove(p); System.out.println(p.hashCode()); System.out.println(map.size()); } } class Person {private String name;privateint age;public Person(String name,int age) {super();this.name = name;this.age = age; }public String getName() {return name; }publicvoid setName(String name) {this.name = name; }publicint getAge() {return age; }publicvoid setAge(int age) {this.age = age; } @Overridepublicboolean equals(Object obj) {// TODO Auto-generated method stubreturnsuper.equals(obj); } @Overridepublicint hashCode() {return name.hashCode() * 123; } }
内存泄漏很详细的文章
https://www.jb51.net/article/92311.htm