import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
/**
* hashCode的作用
*
* @author 张明学
*
*/
public class HashCodeStudy {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
Point p1 = new Point(3, 4);
Point p2 = new Point(4, 7);
Point p3 = new Point(3, 4);
System.out.println(p1.equals(p3));// 重写equals前:false
System.out.println(p1.equals(p3));// 重写equals后:true
Collection collection1 = new ArrayList();
collection1.add(p1);
collection1.add(p2);
collection1.add(p3);
collection1.add(p1);
Collection collection2 = new HashSet();
collection2.add(p1);
collection2.add(p2);
collection2.add(p3);
collection2.add(p1);
// 无论是否为重复放,大小为4
System.out.println(collection1.size());
// 对于set若重复(两个p1重复)放则放不进去,大小为3
System.out.println(collection2.size());
// 若重写equest方法并且重写了hashCode方法即x与y的值相同则让它们的equest和hashCode相等,
// 这两个对象equest相等并放在同一块内存中,即p1与p3重复只能放进去一个。
// **注意**:对于hashSet而言,并不是所有对象放在一块内存中,根据对象的hash值将它们的分到不同的内存块中
// 若没有重写hashCode方法,若p1与p3的hashCode值不在同一块内存中,这们虽然它们的equest相同但它们不一定
// 重复。(即p1与p3有可能能同放入hashSet中)
// 一般情况我们会让两个对的equest方法相同时也会让它们的hashCode相同,就不会出现上述情况
System.out.println(collection2.size());// 这里我们让poin类的equest与hashCode相同,故大小为2(p1与p3重复)
// 由于对于hash类的集合是根据对象的hash值然后计算再放入内存中,若将对象放入hash类的集合中后修改了对象参入了
// 计算hash值的属性值(如与x或y的值),即改变了对象的hash值则该对象有可能就删除不掉!!!因为hash值改变了
// 删除的时候根据后来的hash值计算出该对象在内存中所存放的内存块(有可能与放入之前不是同一块内存)找该对象,
// 就找不到了,故也删除不掉。如下例:
p2.x = 24;// 修改了x的值也改变了p2的hashCode值
collection2.remove(p2);// 删除p2
System.out.println(collection2.size());// 大小还是为2与上面的大小一样,没有删除掉!!!
// 上面的情况也就引起了Java中的内存泄露!!!
}
}
public class Point {
public int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + x;
result = PRIME * result + y;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Point other = (Point) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
}