标题: 关于hashset的一点疑问 [打印本页] 作者: xuchulong1 时间: 2012-10-29 23:02 标题: 关于hashset的一点疑问 这里定义一个点(是张老师视频里的)
public class ReflectPoint
{
@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;
ReflectPoint other = (ReflectPoint) obj;
if (x != other.x)
return false;
if (y != other.y)
return false;
return true;
}
private int x;
public int y;
public ReflectPoint(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
public static void main(String[] args)
{
Collection c = new HashSet();
ReflectPoint p1 = new ReflectPoint(3,3);
ReflectPoint p2 = new ReflectPoint(3,5);
ReflectPoint p3 = new ReflectPoint(3,3);