问题
半夜睡不着怎么办,码呗! 》_《
描述
给出一个IntStream请把它按照奇数偶数规则分解成2个部分
代码
import javafx.util.Pair;
import java.util.LinkedList;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.stream.IntStream;
public class Main {
public static IntStream getNumbers(int len, int max) {
Random r = new Random();
return IntStream.rangeClosed(1, len).map((x) -> r.nextInt(max));
}
public static void main(String[] args) {
// 被逼无奈 写了个辅助方法
BiFunction<LinkedList<Integer>, Integer, LinkedList<Integer>> f =
(list, i) -> list.add(i) ? list : list;
// 加上 .parallel() 就并行了
Pair<LinkedList<Integer>, LinkedList<Integer>> p = getNumbers(100000, 100)
.mapToObj((x) -> new Pair<>((x & 1) == 0, x))
.collect(
() -> new Pair<>(new LinkedList<>(), new LinkedList<>()),
(Pair<LinkedList<Integer>,
LinkedList<Integer>> ps, Pair<Boolean, Integer> pe) -> {
if(pe.getKey())
new Pair<>(f.apply(ps.getKey(), pe.getValue()), ps.getValue());
else
new Pair<>(ps.getKey(), f.apply(ps.getValue(), pe.getValue()));
},
(Pair<LinkedList<Integer>,
LinkedList<Integer>> ps1,
Pair<LinkedList<Integer>,
LinkedList<Integer>> ps2) ->
new Pair<>(ps1.getKey().addAll(ps2.getKey()),
ps1.getValue().addAll(ps2.getValue()))
);
// 不写.stream() 也行
p.getKey().forEach(System.out::println);
System.out.println("--------------");
p.getValue().forEach(System.out::println);
}
}
吐槽
代码写长了 真难排版