Java Stream入门Demo

以下采用 Junit测试用例。

 @Test
    public void testStreamForEach() {
        IntStream stream = IntStream.of(1, 1, 2, 3, 4, 5);
        stream.distinct().map(operand -> operand + 10).distinct().forEach(value ->
                System.out.println(value)
        );
        Assert.assertEquals(2, 2);
    }

    @Test
    public void testStreamFilter() {
        IntStream stream = IntStream.of(1, 2, 3, 4, 5);
        stream.filter(value -> {
            System.out.println(value);
            return value > 3;
        }).forEach(
                value -> {
                    System.out.println("1111: " + value);

                }
        );
        ;
        Assert.assertArrayEquals(new int[]{4, 5,}, new int[]{4, 5,});
    }

    @Test
    public void testStreamSkip() {
        IntStream stream = IntStream.of(1, 2, 3, 4, 5);
        long count = stream.skip(2).count();
        Assert.assertEquals(3, count);
    }

    @Test
    public void testStreamMap() {
        IntStream first = IntStream.of(1, 2, 3, 4, 5);
        IntStream second = IntStream.of(1, 2, 3, 4, 5);
        int sum1 = first.sum();
        int sum2 = second.map(
                operand -> operand + 10
        ).sum();
        Assert.assertEquals(sum2 - sum1, 50);
    }

    @Test
    public void testStreamMin() {
        IntStream stream = IntStream.of(1, 2, 3, 4, 5);
        Assert.assertEquals(1, stream.min().getAsInt());
    }

    @Test
    public void testStreamMax() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        Assert.assertEquals(6, stream.max().getAsInt());
    }

    @Test
    public void testStreamNoneMatch01() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        boolean result = stream.noneMatch(
                value -> value > 10
        );
        Assert.assertEquals(result, true);
    }

    @Test
    public void testStreamNoneMatch02() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        boolean result = stream.noneMatch(
                value -> value > 1
        );
        Assert.assertEquals(result, false);
    }

    @Test
    public void testStreamAnyMatch01() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        boolean result = stream.anyMatch(
                value -> value > 1
        );
        Assert.assertEquals(result, true);
    }

    @Test
    public void testStreamAnyMatch02() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        boolean result = stream.anyMatch(
                value -> value > 10
        );
        Assert.assertEquals(result, false);
    }

    @Test
    public void testStreamAnyMatch03() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        boolean result = stream.anyMatch(
                value -> value > 5
        );
        Assert.assertEquals(result, true);
    }

    @Test
    public void testStreamAllMatch01() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        boolean result = stream.allMatch(
                value -> value > 5
        );
        Assert.assertEquals(result, false);
    }

    @Test
    public void testStreamAllMatch02() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        boolean result = stream.allMatch(
                value -> value > 0
        );
        Assert.assertEquals(result, true);
    }

    @Test
    public void testStreamSorted01() {
        IntStream stream = IntStream.of(1, 2, 6, 3, 4, 5);
        int[] result = stream.sorted().toArray();
        Assert.assertArrayEquals(result, new int[]{1, 2, 3, 4, 5, 6});
    }


    @Test
    public void testStreamReduce01() {
        IntStream stream = IntStream.of(1, 2, 3, 4, 5);
        int result = stream.reduce(0, (left, right) ->
                left + right
        );
        Assert.assertEquals(15, result);
    }






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值