我有以下方法(见下文).代码正在运行,但我得到了很多重复,我应该使用IntStream.
你能指定一下如何更好地优化代码吗?提前致谢.
public static List<Integer> oddOrEven(List<Integer> integers) {
long sum = integers.stream().mapToInt(i ->i).summaryStatistics().getSum();
if (sum % 2 == 0) {
return integers.stream().filter(x -> x % 2==0).distinct().collect(Collectors.toList());
} else if (sum % 2 != 0) {
return integers.stream().filter(x -> x % 2 != 0).distinct().collect(Collectors.toList());
}
return null;
}
Run Code Online (Sandbox Code Playgroud)