包含对列表

发布于 2024-10-16 10:38:15 字数 204 浏览 2 评论 0原文

  List<Pair<String, String> > lp = new ArrayList<Pair<String, String> >();
  lp.add(new Pair("1", "2"));

我应该如何检查列表 lp 是否包含 1 和 2 即 Pair ("1", "2")。

  List<Pair<String, String> > lp = new ArrayList<Pair<String, String> >();
  lp.add(new Pair("1", "2"));

How should I check if the list lp contains 1 and 2 i.e the Pair ("1", "2").

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光①夏 2024-10-23 10:38:15

您的 Pair 类需要实现 equals()hashCode() 并且您已准备就绪。 List.contains() 是根据类型的 equals() 方法实现的。请参阅 List.contains() 的 API。 (编辑了一些来自 @maaartinus 的评论,你应该阅读他的答案,因为观察结果是可靠的,对我来说将它们折叠在这里有点荒谬。正如 maaartinus 指出的,这里的最佳实践是避免容易出错的 equals 和 hashcode 手动定义,而是基于 Guava 的辅助函数构建 可空等于n 个对象的哈希码)。

final class Pair<T> {

   final T left;
   final T right;

   public Pair(T left, T right)
   {
     if (left == null || right == null) { 
       throw new IllegalArgumentException("left and right must be non-null!");
     }
     this.left = left;
     this.right = right;
   }

   public boolean equals(Object o)
   {
     // see @maaartinus answer
     if (! (o instanceof Pair)) { return false; }
     Pair p = (Pair)o;
     return left.equals(p.left) && right.equals(p.right);
   } 

   public int hashCode()
   {
      return 7 * left.hashCode() + 13 * right.hashCode();
   } 
}

使用合适的 equals(),您现在可以执行以下操作:

  lp.add(new Pair("1", "2"));
  assert lp.contains(new Pair("1","2"));

回应下面的评论,也许最好包含一个很好的参考“为什么我需要实现 hashCode()?”

Your Pair class needs to implement equals() and hashCode() and you're all set. List.contains() is implemented in terms of the type's equals() method. See the API for List.contains(). (Edited a bit to address comments from @maaartinus, whose answer you should read b/c the observations are solid, and it's a bit ridiculous for me to fold them in here. As maaartinus points out, a best-practice here would be to avoid error-prone manual definitions for equals and hashcode, and instead build on Guava's helper functions for nullable equals and hashCode for n objects).

final class Pair<T> {

   final T left;
   final T right;

   public Pair(T left, T right)
   {
     if (left == null || right == null) { 
       throw new IllegalArgumentException("left and right must be non-null!");
     }
     this.left = left;
     this.right = right;
   }

   public boolean equals(Object o)
   {
     // see @maaartinus answer
     if (! (o instanceof Pair)) { return false; }
     Pair p = (Pair)o;
     return left.equals(p.left) && right.equals(p.right);
   } 

   public int hashCode()
   {
      return 7 * left.hashCode() + 13 * right.hashCode();
   } 
}

With suitable equals(), you can now do:

  lp.add(new Pair("1", "2"));
  assert lp.contains(new Pair("1","2"));

Responding to the comments below, perhaps it would be good to include a good reference for "Why do I need to implement hashCode()?"

陈独秀 2024-10-23 10:38:15

andersoj 的答案中的实现

 return left != null && right != null && left.equals(p.left) && right.equals(p.right);

是错误的: null 测试清楚地表明 null 是 left 和 right 的合法值。因此,至少存在两个问题:

  • new Pair(null, null).hashCode() 抛出 NPE
  • new Pair(null, null) 不等于自身!

查看 Guava 类对象以获得正确的实现。使用它或编写静态帮助器方法

public static boolean equal(Object a, Object b) {
    return a==b || a!=null && a.equals(b);
}
public static int hashCode(Object a) {
    return a==null ? 0 : a.hashCode();
}

,并始终使用它们。

永远不要编写包含 null 测试的 equals

这很容易搞砸,而且没有人注意到它。使用 Helper,正确处理它很简单:

public boolean equals(Object o)  {
    if (!(o instanceof Pair)) return false;
    Pair p = (Pair) o;
    return Helper.equals(left, p.left) && Helper.equals(right, p.right);
} 

public int hashCode() {
    return 7 * Helper.hashCode(left) + 13 * Helper.hashCode(right);
} 

当然,在构造函数中禁止 null 也是一种选择。

The implementation in the answer by andersoj

 return left != null && right != null && left.equals(p.left) && right.equals(p.right);

is wrong: The null tests clearly suggest that null is a legal value for left and right. So there are at least two problems there:

  • new Pair(null, null).hashCode() throws NPE
  • new Pair(null, null) does NOT equal to itself!

Have a look at Guava class Objects for a correct implementation. Use it or write a static helper methods like

public static boolean equal(Object a, Object b) {
    return a==b || a!=null && a.equals(b);
}
public static int hashCode(Object a) {
    return a==null ? 0 : a.hashCode();
}

and always use them.

Never ever write equals containing a null test.

It's to easy to blow it, and nobody noticed it. Using the Helper, it's trivial to get it right:

public boolean equals(Object o)  {
    if (!(o instanceof Pair)) return false;
    Pair p = (Pair) o;
    return Helper.equals(left, p.left) && Helper.equals(right, p.right);
} 

public int hashCode() {
    return 7 * Helper.hashCode(left) + 13 * Helper.hashCode(right);
} 

Of course, forbidding nulls in the constructor is an option, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文