使用其他所有数组元素筛选数组-Java 8
我有一个如下的字符串数组:使用其他所有数组元素筛选数组-Java 8,java,java-8,java-stream,Java,Java 8,Java Stream,我有一个如下的字符串数组: firstArray = {"1", "2", "3", "4" }; secondArray = {"2", "5", "6", "7" }; firstArray.stream() .filter(element -> !element.equals("2")) .forEach((element) -> { finalArrayList.add(element); } ); List<String&
firstArray = {"1", "2", "3", "4" };
secondArray = {"2", "5", "6", "7" };
firstArray.stream()
.filter(element -> !element.equals("2"))
.forEach((element) -> {
finalArrayList.add(element);
}
);
List<String> result = Arrays.stream(firstArray)
.filter(el -> Arrays.stream(secondArray).noneMatch(el::equals))
.collect(Collectors.toList());
第二个数组如下:
firstArray = {"1", "2", "3", "4" };
secondArray = {"2", "5", "6", "7" };
firstArray.stream()
.filter(element -> !element.equals("2"))
.forEach((element) -> {
finalArrayList.add(element);
}
);
List<String> result = Arrays.stream(firstArray)
.filter(el -> Arrays.stream(secondArray).noneMatch(el::equals))
.collect(Collectors.toList());
如果我想用一个元素流,我可以这样做:
firstArray = {"1", "2", "3", "4" };
secondArray = {"2", "5", "6", "7" };
firstArray.stream()
.filter(element -> !element.equals("2"))
.forEach((element) -> {
finalArrayList.add(element);
}
);
List<String> result = Arrays.stream(firstArray)
.filter(el -> Arrays.stream(secondArray).noneMatch(el::equals))
.collect(Collectors.toList());
如何将java 8中的第一个数组与第二个数组中的所有元素进行流式处理?您到底想做什么?是否获取第一个数组中同时存在于第二个数组中的所有元素 像这样:
String[] firstArray = {"1", "2", "3", "4"};
String[] secondArray = {"2", "5", "6", "7"};
List<String> finalArrayList = Arrays.stream(firstArray)
.filter(element -> arrayContains(secondArray, element))
.collect(Collectors.toList());
你到底想做什么?是否获取第一个数组中同时存在于第二个数组中的所有元素 像这样:
String[] firstArray = {"1", "2", "3", "4"};
String[] secondArray = {"2", "5", "6", "7"};
List<String> finalArrayList = Arrays.stream(firstArray)
.filter(element -> arrayContains(secondArray, element))
.collect(Collectors.toList());
如果使用
流API
只保留第一个数组中没有的元素,可以这样做:
firstArray = {"1", "2", "3", "4" };
secondArray = {"2", "5", "6", "7" };
firstArray.stream()
.filter(element -> !element.equals("2"))
.forEach((element) -> {
finalArrayList.add(element);
}
);
List<String> result = Arrays.stream(firstArray)
.filter(el -> Arrays.stream(secondArray).noneMatch(el::equals))
.collect(Collectors.toList());
List result=Arrays.stream(firstArray)
.filter(el->Arrays.stream(secondArray).noneMatch(el::equals))
.collect(Collectors.toList());
如果您想使用流API只保留第一个数组中没有的元素,您可以这样做:
firstArray = {"1", "2", "3", "4" };
secondArray = {"2", "5", "6", "7" };
firstArray.stream()
.filter(element -> !element.equals("2"))
.forEach((element) -> {
finalArrayList.add(element);
}
);
List<String> result = Arrays.stream(firstArray)
.filter(el -> Arrays.stream(secondArray).noneMatch(el::equals))
.collect(Collectors.toList());
List result=Arrays.stream(firstArray)
.filter(el->Arrays.stream(secondArray).noneMatch(el::equals))
.collect(Collectors.toList());
使用staticStream.concat(Stream,Stream)
我不明白这个问题。你想要实现什么?这里的预期结果是什么?使用staticStream.concat(Stream,Stream)
我不理解这个问题。你想要实现什么?这里的预期结果是什么?请注意anyMatch(谓词)
的存在,它优先于filter(谓词).findFirst().isPresent()
。此外,如果您只对匹配的存在感兴趣,findAny()
将比findFirst()
更合适……请注意anyMatch(谓词)
的存在,它比过滤器(谓词).findFirst().isPresent()更可取。此外,如果您只对匹配的存在感兴趣,findAny()
将比findFirst()
更合适…