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);
    }






你可能感兴趣的:(android,kotlin,android,studio)