Java如何执行Collections.contains()方法?有没有人能够提供一些相关链接?

huangapple go评论102阅读模式
英文:

How does Java execute the Collections.contains() method, Could anyone pls show some links to this?

问题

遇到了contains()方法的问题,不确定为什么在这两种情况下表现不同,如下面的代码片段所示:

  1. import java.awt.*;
  2. import java.util.LinkedHashSet;
  3. import java.util.Set;
  4. public class SetDemo01 {
  5. public static void main(String[] args) {
  6. Set<Point> set = new LinkedHashSet<>();
  7. Point a = new Point(0, 1);
  8. Point b = new Point(0, 1);
  9. set.add(a);
  10. System.out.println(set.contains(b));
  11. Set<Coordinate> set02 = new LinkedHashSet<>();
  12. Coordinate c = new Coordinate(0, 1);
  13. Coordinate d = new Coordinate(0, 1);
  14. set02.add(c);
  15. System.out.println(set02.contains(d));
  16. }
  17. }
  18. class Coordinate {
  19. int x;
  20. int y;
  21. Coordinate(int x, int y) {
  22. this.x = x;
  23. this.y = y;
  24. }
  25. }

控制台输出:

  1. true
  2. 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:

  1. import java.awt.*;
  2. import java.util.LinkedHashSet;
  3. import java.util.Set;
  4. public class SetDemo01 {
  5. public static void main(String[] args) {
  6. Set&lt;Point&gt; set = new LinkedHashSet&lt;&gt;();
  7. Point a = new Point(0, 1);
  8. Point b = new Point(0, 1);
  9. set.add(a);
  10. System.out.println(set.contains(b));
  11. Set&lt;Coordinate&gt; set02 = new LinkedHashSet&lt;&gt;();
  12. Coordinate c = new Coordinate(0, 1);
  13. Coordinate d = new Coordinate(0, 1);
  14. set02.add(c);
  15. System.out.println(set02.contains(d));
  16. }
  17. }
  18. class Coordinate {
  19. int x;
  20. int y;
  21. Coordinate (int x, int y) {
  22. this.x = x;
  23. this.y = y;
  24. }
  25. }

The console prints:

  1. true
  2. 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

如果你像这样声明你的类,你将得到你期望的输出:

  1. class Coordinate {
  2. int x;
  3. int y;
  4. Coordinate (int x, int y) {
  5. this.x = x;
  6. this.y = y;
  7. }
  8. @Override
  9. public boolean equals(Object o) {
  10. if (this == o) return true;
  11. if (o == null || getClass() != o.getClass()) return false;
  12. Coordinate that = (Coordinate) o;
  13. return x == that.x && y == that.y;
  14. }
  15. @Override
  16. public int hashCode() {
  17. return Objects.hash(x, y);
  18. }
  19. }

如果不重写 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:

  1. class Coordinate {
  2. int x;
  3. int y;
  4. Coordinate (int x, int y) {
  5. this.x = x;
  6. this.y = y;
  7. }
  8. @Override
  9. public boolean equals(Object o) {
  10. if (this == o) return true;
  11. if (o == null || getClass() != o.getClass()) return false;
  12. Coordinate that = (Coordinate) o;
  13. return x == that.x &amp;&amp;
  14. y == that.y;
  15. }
  16. @Override
  17. public int hashCode() {
  18. return Objects.hash(x, y);
  19. }
  20. }

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

huangapple
  • 本文由 发表于 2020年8月27日 14:42:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63610501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定