我是相当新的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
谢谢!
我是相当新的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
谢谢!
只需使用Collections.disjoint。 这种方法检查两个阵列的任何元件是常见的。
Collections.disjoint(Arrays.asList(array1), Arrays.asList(array2))
我觉得这对你的作品。 然而,我不得不第二阵列转换成一组检查,如果在阵列中存在的元素。 我认为这不是一个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);