public boolean contains(Object o) {
return map.containsKey(o);
}
containsKey :
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}
는 getNode : arrays
의 equals
가의 비교 ==
을 할 것이기 때문에
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
그것은 작동하지 않습니다, 그것은 그들이 가리 경우에만 true를 돌려줍니다 같은 인스턴스.
귀하의 문제는 (적어도 나를 위해 (문제가 될 수 있습니다.) 내가하는 쉬운 방법을 선호)
저장하기 때문에 (두 개의 배열 요소를 비교하고 참조 할 수있는 방법) Arrays.equals
와 함께 작업없이 고정 할 수
X
및 Y
좌표, 그냥 Point
public class Point {
public final int X;
public final int Y;
public Point(int x, int y)
{
X = x;
Y = y;
}
@Override
public boolean equals(Object obj)
{
if (obj == this) {
return true;
}
if (obj instanceof Point) {
Point pObj = (Point) obj;
return pObj.X == X && pObj.Y == Y;
}
return false;
}
@Override
public int hashCode()
{
int result = X;
result = 31 * result + Y;
return result;
}
}
다음 X, Y
점을 저장 Point
클래스를 사용하는 클래스를 만든다.
사용자 정의 포인트 클래스를 만드는 대신 Android Point
을 사용할 수 있습니다.
예제 HashSet
의 javadoc에서
Set<Point> points = new HashSet<Point>();
points.add(new Point(1, 3));
points.add(new Point(1, 4));
System.out.println(points.contains(new Point(1, 3)));
System.out.println(points.contains(new Point(1, 4)));
System.out.println(points.contains(new Point(1, 5)));
int []의 해시가 내용이 아닌 객체 인스턴스와 관련되어 있기 때문에 – njzk2