简单的方法来找到如果两个不同的列表包含完全相同的元素? [英] Simple way to find if two different lists contain exactly the same elements?

查看:218
本文介绍了简单的方法来找到如果两个不同的列表包含完全相同的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准Java库中,如果两个列表包含完全相同的元素,最简单的方法是什么?

What is the simplest way to find if two Lists contain exactly the same elements, in the standard Java libraries?

如果这两个列表是相同的实例,那就没有关系,如果列表的类型参数不同也不要紧。

It shouldn't matter if the two Lists are the same instance or not, and it shouldn't matter if the type parameter of the Lists are different.

例如

List list1
List<String> list2; 
// ... construct etc

list1.add("A");
list2.add("A"); 
// the function, given these two lists, should return true

我在脸上我知道: - )

There's probably something staring me in the face I know :-)

编辑:为了澄清,我一直在寻找EXACT相同的元素和数字

To clarify, I was looking for the EXACT same elements and number of elements, in order.

编辑:感谢您指出我无法看到的明显答案: )

Thanks for pointing out the obvious answer I couldn't see for looking :-)

虽然到目前为止所有答案都是正确的,但有些答案比其他答案更正确,所以在接受之前,我会先等一段时间才能得到最佳的四舍五入答案。

Although all the answers given so far are correct, some are more correct than others, so I'll wait a while for the best rounded-off answer before accepting.

推荐答案

如果你关心订单,那么只需使用equals方法:

If you care about order, then just use the equals method:

list1.equals(list2)


比较指定的对象与
此列表的相等性。返回true
当且仅当指定的对象是
也是一个列表,两个列表都具有相同的
大小,并且两个列表中的所有对应的
元素都是相等的。
(如果
(e1 == null?e2 == null:
e1.equals(e2)),两个元素e1和e2相等。)换句话说,两个
如果它们
在相同的
顺序中包含相同的元素,则列表被定义为相等。这个定义确保
的equals方法在
的List接口的不同实现中正确地工作

Compares the specified object with this list for equality. Returns true if and only if the specified object is also a list, both lists have the same size, and all corresponding pairs of elements in the two lists are equal. (Two elements e1 and e2 are equal if (e1==null ? e2==null : e1.equals(e2)).) In other words, two lists are defined to be equal if they contain the same elements in the same order. This definition ensures that the equals method works properly across different implementations of the List interface.



<如果你想检查独立的订单,你可以复制所有的元素到集合,并使用等于结果集:

If you want to check independent of order, you could copy all of the elements to Sets and use equals on the resulting Sets:

public static <T> boolean listEqualsNoOrder(List<T> l1, List<T> l2) {
    final Set<T> s1 = new HashSet<>(l1);
    final Set<T> s2 = new HashSet<>(l2);

    return s1.equals(s2);
}

这种方法的一个警告是,它不会检查重复。例如:如果list1是[A,B,A],list2是[A,B,B] Set方法会说他们是平等的。

One caveat with this approach is that it won't check duplicates exactly. eg: if list1 was ["A", "B", "A"] and list2 was ["A", "B", "B"] the Set approach would say they were equal.

如果您不希望将它们视为相等,但不关心顺序,您可以先对两个列表进行排序,然后再对它们进行比较,作为Set方法,但使用 Multiset (不是标准库的一部分,但 Google Guava 有一个好的。

If you don't want these to be treated as equal but you don't care about order you can either sort both lists before comparing them or you could do the same thing as the Set approach but with a Multiset (not part of the standard libraries, but Google Guava has a good one.

这篇关于简单的方法来找到如果两个不同的列表包含完全相同的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆