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

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

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&lt;Point&gt; set = new LinkedHashSet&lt;&gt;();
        Point a = new Point(0, 1);
        Point b = new Point(0, 1);

        set.add(a);
        System.out.println(set.contains(b));

        Set&lt;Coordinate&gt; set02 = new LinkedHashSet&lt;&gt;();
        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 &amp;&amp;
          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

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:

确定