【Java】深夜代码祭(1)

问题

半夜睡不着怎么办,码呗! 》_《

描述

给出一个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);
    }
}

吐槽

代码写长了 真难排版

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值