我对java流还很陌生,想知道是否有一种简单的解决方案可以使用java流检查数组1中是否还存在数组2
示例:
array1 = ["banana","apple","cat"]
array2 = ["toast","bread","pizza","banana"]
--> return true
array1 = ["banana","apple","cat"]
array2 = ["toast","bread","pizza"]
--> return false
谢谢!
答案 0 :(得分:4)
只需使用Collections.disjoint。此方法检查两个数组中的任何元素是否相同。
Collections.disjoint(Arrays.asList(array1), Arrays.asList(array2))
答案 1 :(得分:1)
我认为这对您有用。但是,我必须将第二个数组转换为set检查数组中是否存在元素。我认为这比for循环迭代更直观。
String[] arr1 = new String[]{"a", "b"};
String[] arr2 = new String[]{"a", "d"};
Set<String> strings = Set.of(arr2);
boolean result = Stream.of(arr1).anyMatch(strings::contains);