StreamAPI部分接口

package com.yang.springwebflux;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Slf4j
public class StreamApiTest extends SpringWebFluxApplicationTests {

    /**
     * 在streamApi中读写磁盘文件
     *
     * @throws IOException
     */
    @Test
    public void readwrite() throws IOException {
        //0.初始化文件路径和数据流
        Path path = Paths.get("D:\\list.txt");
        IntStream range = IntStream.range(0, 5000);

        //1.将集合数据写入磁盘文件
        try (PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(path))) {
            range.forEach(printWriter::println);
        }

        //2.从文件读取行数据,然后【并行】打印,可以看到fork-join机制生效
        Files.lines(path).parallel().forEach(log::info);
    }

    /**
     * stream流数据分区,类似filter操作,只是将满足筛选条件和不满足的分别放到两个集合中
     */
    @Test
    public void partitionBy() {
        ArrayList<Integer> list0 = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
            add(4);
        }};

        ArrayList<Integer> list1 = new ArrayList<Integer>() {{
            add(5);
            add(6);
            add(7);
            add(8);
        }};

        ArrayList<ArrayList<Integer>> outer = new ArrayList<>(5);
        outer.add(list0);
        outer.add(list1);

        list.stream().collect().flatMap(it -> it.stream()).collect(Collectors.toList()).forEach(System.out::println);
    }

    /**
     * stream元素分组
     */
    @Test
    public void groupingBy() {
        List<String> list = new ArrayList<String>() {{
            add("1");
            add("1");
            add("2");
            add("2");
            add("3");
            add("3");
        }};
        list.stream().collect(Collectors.groupingBy(it -> it)).forEach((k, v) -> System.out.println(k + v));
    }

    /**
     * stream流数据分区,类似filter操作,只是将满足筛选条件和不满足的分别放到两个集合中
     */
    @Test
    public void partitionBy() {
        ArrayList<Integer> list = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
            add(4);
        }};
        list.stream().collect(Collectors.partitioningBy(it -> (it % 2) == 0)).forEach((k, v) -> System.out.println(k + "--" + v));
    }

    /**
     * 数据流概要统计
     */
    @Test
    public void summary() {
        //0.初始化一个集合
        ArrayList<Integer> list = new ArrayList<Integer>() {{
            add(1);
            add(2);
            add(3);
            add(4);
        }};

        //1.集合的统计对象包含:count, sum, min, average, max属性
//        DoubleSummaryStatistics collect = list.stream().collect(Collectors.summarizingDouble(it -> it));
        IntSummaryStatistics collect = list.stream().collect(Collectors.summarizingInt(it -> it));
        System.out.println(collect);

        //2.向统计对象中添加新元素,此时全局的统计结果将收影响
        collect.accept(100);
        System.err.println(collect);
    }

    /**
     * 统计操作:count, sum, min, average, max
     */
    @Test
    public void statistics() {
        OptionalInt max = IntStream.range(0, 12).max();
        OptionalInt min = IntStream.range(0, 12).min();
        long count = IntStream.range(0, 12).count();
        OptionalDouble average = IntStream.range(0, 12).average();
        int sum = IntStream.range(0, 12).sum();
        System.out.println(max);
        System.out.println(min);
        System.out.println(count);
        System.out.println(average);
        System.out.println(sum);
    }

    /**
     * 流数据的匹配操作
     */
    @Test
    public void match() {
        Supplier<IntStream> range = () -> IntStream.range(0, 10);
        boolean allMatch = range.get().allMatch(it -> (it % 2) == 0);
        System.out.println(allMatch);
        boolean noneMatch = range.get().noneMatch(it -> (it % 2) == 0);
        System.out.println(noneMatch);
        boolean anyMatch = range.get().anyMatch(it -> (it % 2) == 0);
        System.out.println(anyMatch);
    }
}
posted @ 2020-03-23 13:01  JaxYoun  阅读(118)  评论(0编辑  收藏  举报