英文:
How does Java execute the Collections.contains() method, Could anyone pls show some links to this?
问题
遇到了contains()
方法的问题,不确定为什么在这两种情况下表现不同,如下面的代码片段所示:
import java.awt.*;
import java.util.LinkedHashSet;
import java.util.Set;
public class SetDemo01 {
public static void main(String[] args) {
Set<Point> set = new LinkedHashSet<>();
Point a = new Point(0, 1);
Point b = new Point(0, 1);
set.add(a);
System.out.println(set.contains(b));
Set<Coordinate> set02 = new LinkedHashSet<>();
Coordinate c = new Coordinate(0, 1);
Coordinate d = new Coordinate(0, 1);
set02.add(c);
System.out.println(set02.contains(d));
}
}
class Coordinate {
int x;
int y;
Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
}
控制台输出:
true
false
现在我知道我需要重写equals()
和hashCode()
方法,但能否有人请出示参考:在应用contains()
方法时,会执行equals()
,并比较hashCode
。
英文:
Met an issue with the contains()
method, not sure why it behaves differently in the two scenarios, as the following code snippet shows:
import java.awt.*;
import java.util.LinkedHashSet;
import java.util.Set;
public class SetDemo01 {
public static void main(String[] args) {
Set<Point> set = new LinkedHashSet<>();
Point a = new Point(0, 1);
Point b = new Point(0, 1);
set.add(a);
System.out.println(set.contains(b));
Set<Coordinate> set02 = new LinkedHashSet<>();
Coordinate c = new Coordinate(0, 1);
Coordinate d = new Coordinate(0, 1);
set02.add(c);
System.out.println(set02.contains(d));
}
}
class Coordinate {
int x;
int y;
Coordinate (int x, int y) {
this.x = x;
this.y = y;
}
}
The console prints:
true
false
Now I knew I need to override the equals()
& hashCode()
methods, but could someone pls show the reference for this: when apply the contains()
method, the equals()
is run, and the 'hashCode' is compared.
答案1
得分: 4
如果你像这样声明你的类,你将得到你期望的输出:
class Coordinate {
int x;
int y;
Coordinate (int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Coordinate that = (Coordinate) o;
return x == that.x && y == that.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
如果不重写 equals
方法,contains
方法将只检查两个对象是否是相同的引用。
请也参考:https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java
英文:
If you declare your class like that, you would have your expected output:
class Coordinate {
int x;
int y;
Coordinate (int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Coordinate that = (Coordinate) o;
return x == that.x &&
y == that.y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
Without overriding the equals
method the contains
method will only look if the two objects are the same reference.
Please have also a look at: https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论