[英]Check if two lists (one is nested) are mutually exclusive
I want to compare two list (one is nested) for mutual exclusivity.我想比较两个列表(一个是嵌套的)的互斥性。 Problem is that this code is printing false even if they have only one element in common.
问题是即使它们只有一个共同元素,这段代码也会打印错误。 I need it to print false if they have both elements in common.
如果它们具有共同的两个元素,我需要它打印 false 。
output I'm getting: false true false
output 我得到:
false true false
Desired output: true true false
所需 output:
true true false
...
ArrayList<String> properties = new ArrayList<>(Arrays.asList("A", "B"));
ArrayList<ArrayList<String> > pairs = new ArrayList<ArrayList<String> >();
pairs.add(new ArrayList<>(Arrays.asList("A", "C")));
pairs.add(new ArrayList<>(Arrays.asList("D", "C")));
pairs.add(new ArrayList<>(Arrays.asList("A", "B")));
for(int i = 0; i< pairs.size(); i++) {
System.out.println(Collections.disjoint(properties, pairs.get(i)));
}
So, as I understand your question now after your edit, you are making it too difficult.所以,正如我在编辑后现在理解你的问题一样,你让它变得太难了。 There is no need for
Collections.disjoint()
here, if you are only looking for non-equal lists.如果您只是在寻找不相等的列表,那么这里不需要
Collections.disjoint()
。 If you want to print false
if you come across a List with identical contents, why not simply use equals()
?如果你想在遇到内容相同的列表时打印
false
,为什么不简单地使用equals()
呢?
So the check becomes所以支票变成了
for (int i = 0; i < pairs.size(); i++) {
System.out.println(!properties.equals(pairs.get(i)));
}
printing the following output打印以下内容 output
true
true
false
Collection.containsAll()
I want to compare two list (one is nested) for mutual exclusivity.
我想比较两个列表(一个是嵌套的)的互斥性。 Problem is that this code is printing
false
even if they have only one element in common.问题是即使它们只有一个共同元素,这段代码也会打印
false
。 I need it to printfalse
if they have both elements in common .如果它们具有共同的两个元素,我需要它打印
false
。
If you want to make sure if two Lists having equal size contain the same elements regardless of their order, then you can make use of the method Collection.containsAll()
.如果您想确定两个具有相同大小的列表是否包含相同的元素而不管它们的顺序如何,那么您可以使用方法
Collection.containsAll()
。
List<String> properties = List.of("A", "B");
List<List<String>> pairs = List.of(
List.of("A", "C"), List.of("D", "C"), List.of("A", "B")
);
for (List<String> pair : pairs) {
System.out.println(!properties.containsAll(pair));
}
equals
method of all implementations of the List
interface would return true
only if two lists contain the same elements in the same order (if you don't want the order to be taken into account, then equals
is not suitable for this task).仅当两个列表包含相同顺序的相同元素时,
List
接口的所有实现的equals
方法才会返回true
(如果您不希望考虑顺序,则equals
不适合此任务)。
Usage of containsAll()
is justifiable only if the list on which you're invoking this method is moderate in size. containsAll()
的使用只有在您调用此方法的列表大小适中时才是合理的。 In this case, it's perfectly acceptable because there are only 2
elements in the list.在这种情况下,这是完全可以接受的,因为列表中只有
2
元素。 Otherwise, it would be performance-wise create a HashSet
based on the properties
list (if all elements are unique) and perform checks against this set (if there could be duplicates in the list we can generate a HashMap
of element-frequencies).否则,基于
properties
列表(如果所有元素都是唯一的)创建一个HashSet
并针对该集合执行检查(如果列表中可能有重复项,我们可以生成HashMap
的元素频率)是性能明智的做法。
You might want to read What does it mean to "program to an interface"?您可能想阅读“对接口编程”是什么意思?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.