简体   繁体   English

Java检查两个列表是否互斥

[英]Java Check if two lists are mutually exclusive

Ok, I have List a and List b 好的,我有List aList b

is there a way to check that no value exist between the two? 有没有办法检查两者之间是否存在价值?

List a // 1,2,4,5
List B // 1,6,7,8

Between both list // 1 FAILURE 两个列表之间// 1 FAILURE

Collections.disjoint(list1, list2)

如果它们没有共同的元素,则返回true

Use Collections.disjoint . 使用Collections.disjoint

Returns true if the two specified collections have no elements in common 如果两个指定的集合没有共同的元素,则返回true

boolean hasCommonElements = Collections.disjoint(listA, listB);

You can use Collections.disjoint() : 您可以使用Collections.disjoint()

public static boolean disjoint(Collection c1, Collection c2): Returns true if the two specified collections have no elements in common. public static boolean disjoint(Collection c1,Collection c2):如果两个指定的集合没有共同的元素,则返回true。

Code: 码:

List<Integer> a = new ArrayList<Integer>();
List<Integer> b = new ArrayList<Integer>();

System.out.println(Collections.disjoint(a, b));

You have to user Collections.disjoint(a, b); 你必须使用Collections.disjoint(a, b); It returns a boolean: true if the lists have no elements in common. 它返回一个布尔值:如果列表没有共同的元素,则返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM