如何检查两个集合是否包含相同的对象

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

How to check whether two sets contain the same objects

问题

我有一个类 IntegerType:

  1. public class IntegerType implements DataType {
  2. private int value;
  3. public IntegerType(int value) {
  4. this.value = value;
  5. }
  6. @Override
  7. public NativeType nativeType() {
  8. return NativeType.INTEGER;
  9. }
  10. @Override
  11. public int size() {
  12. return Integer.BYTES;
  13. }
  14. @Override
  15. public Integer value() {
  16. return this.value;
  17. }
  18. @Override
  19. public boolean equals(Object o) {
  20. boolean result = false;
  21. if (o == null || o.getClass() != getClass()) {
  22. result = false;
  23. } else {
  24. IntegerType integerType = (IntegerType) o;
  25. if (this.value == integerType.value()) {
  26. result = true;
  27. }
  28. }
  29. return result;
  30. }
  31. }

而我实际上想要检查两个包含 IntegerType 实例的集合是否相同,基于这些实例是否具有相同的 .value 参数:

  1. IntegerType int1 = new IntegerType(1);
  2. IntegerType int2= new IntegerType(1);
  3. Set<IntegerType> set1 = new HashSet<>();
  4. set1.add(int1);
  5. Set<IntegerType> set2 = new HashSet<>();
  6. set2.add(int2);
  7. System.out.println(set1.equals(set2));

然而,运行上述代码会输出 false。根据我理解的情况,它应该输出 true,因为 int1.equals(int2)true。我在这里漏掉了什么?

英文:

I have a class IntegerType:

  1. public class IntegerType implements DataType {
  2. private int value;
  3. public IntegerType(int value) {
  4. this.value = value;
  5. }
  6. @Override
  7. public NativeType nativeType() {
  8. return NativeType.INTEGER;
  9. }
  10. @Override
  11. public int size() {
  12. return Integer.BYTES;
  13. }
  14. @Override
  15. public Integer value() {
  16. return this.value;
  17. }
  18. @Override
  19. public boolean equals(Object o) {
  20. boolean result = false;
  21. if (o == null || o.getClass() != getClass()) {
  22. result = false;
  23. } else {
  24. IntegerType integerType = (IntegerType) o;
  25. if (this.value == integerType.value()) {
  26. result = true;
  27. }
  28. }
  29. return result;
  30. }
  31. }

And I essentially want to check whether two sets containing instances of IntegerType are the same based on whether the instances have the same .value parameter:

  1. IntegerType int1 = new IntegerType(1);
  2. IntegerType int2= new IntegerType(1);
  3. Set&lt;IntegerType&gt; set1 = new HashSet&lt;&gt;();
  4. set1.add(int1);
  5. Set&lt;IntegerType&gt; set2 = new HashSet&lt;&gt;();
  6. set2.add(int2);
  7. System.out.println(set1.equals(set2));

However running the above code prints out false. From what I understand it should print true since int1.equals(int2) is true. What am I missing here?

答案1

得分: 2

就像名字所示,HashSet 要求你重写 hashCode 方法,它用来确定两个对象是否可能相等。以下是一种可能的实现:

  1. @Override
  2. public int hashCode() {
  3. final int prime = 31;
  4. int result = 1;
  5. result = prime * result + value;
  6. return result;
  7. }

演示

英文:

As the name suggests, HashSet requires you to override the hashCode method, which it uses to determine whether or not two objects could possibly be equal. Here is a possible implementation:

  1. @Override
  2. public int hashCode() {
  3. final int prime = 31;
  4. int result = 1;
  5. result = prime * result + value;
  6. return result;
  7. }

Demo

huangapple
  • 本文由 发表于 2020年7月27日 22:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63117603.html
匿名

发表评论

匿名网友

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

确定