• 2022-08-08 学习日记(28th day)Stream流


    Stream

    Stream流其实就是一个集合元素的函数模型,他并不是集合,也不是数据结构,其本身并不存储任何的元素或地址(JDK8之后新增的功能)

    使用流的步骤:

    当我们使用一个流的时候,通常包括三个步骤:

    1.获取一个数据源

    2.执行操作获取想要的结果

    3.每次操作,原有的流对象不改变,返回一个新的Stream对象

    Stream的特性:

    1.Stream不会储存数据,一般会输出结果

    2.Stream不会改变数据源,通常情况下会生成一个新的集合

    3.Stream具有延迟执行的特性,只有调用终端操作时,中间操作才会执行。

    相关使用:

    List list = Arrays.asList("a","b","c");

    Arrays.asList是将数组转化成List集合的方法

    ps:

    1.该方法适用于对象型数据的数组(String、Integer...)

    2.该方法不建议使用于基本数据类型的数组(byte,short,int,long,float,double,boolean)

    3.该方法将数组与List列表链接起来:当更新其一个时,另一个自动更新

    4.不支持add()、remove()、clear()等方法
    用此方法得到的List的长度是不可改变的

    创建一个顺序流:

    Stream<String> stream = list.stream();

    创建一个并行流:

    Stream<String> parallelStream = list.parallelStream();

    创建个字符串流:Stream.of();

    Stream<Integer> stream1 = Stream.of(1, 2, 3, 4, 5, 6);

    Stream iterate(final T seed ,final UnaryOperator f):

    指定一个seed常量,生成从seed到常量f(由UnaryOperator返回的值得到)的流。

    1. public static void main(String[] args) {
    2. Stream.iterate(0, n -> n + 1).limit(5).forEach(a -> {
    3. System.out.println(a);
    4. });
    5. }

    根据起始值seed(0),每次生成一个指定递增值(n+1)的数,limit(5)用于截断流的长度,即只获取前5个元素。

    forEach():看起来和咱们增强for循环一样,实际上大概的功能也是这样:打印集合元素:

    .forEach(System.out::println);// 双冒号语法,方法引用

    .findFirst()找到第一个元素

    .findAny();随便找一个

    判断有没有任意一个人大于50岁:(任意匹配)

    boolean b = personList.stream().anyMatch(item -> item.getAge() > 35);

    判断是不是所有人都大于50岁:

    b = personList.stream().allMatch(item -> item.getAge() > 35);

    收集元素:(三个集合):

    1. List<Integer> collect = simpleList.stream().collect(Collectors.toList());
    2. Set<Integer> collect1 = simpleList.stream().collect(Collectors.toSet());
    3. Map<Integer, Integer> map = simpleList.stream()
    4. .collect(Collectors
    5. .toMap(item -> item, item -> item + 1));

    统计:

    1. long count = new Random().ints().limit(50).count();//获取长度
    2. System.out.println(count);
    3. OptionalDouble average = new Random().ints().limit(50).average();//获取五十个的平均数
    4. average.ifPresent(System.out::println);
    5. int sum = new Random().ints().limit(50).sum();//获取五十个随机数的和
    6. System.out.println(sum);

    规约(reduce)缩减,把一个流缩减成一个值

    可以实现对集合的求和,求乘积,求最值

    1. Integer result = simpleList.stream().reduce(1, (n1, n2) -> n1 - n2);
    2. System.out.println(result);

    .collect(Collectors.joining("-"));---字符串拼接用-拼接。

    1. List<String> list = Arrays.asList("A","B","C");
    2. String string = list.stream().collect(Collectors.joining("-"));
    3. System.out.println("拼接后的字符串:" + string);

    按照某一条件分组(分组将集合分为多个map)

    1. // 根据工资分组
    2. Map<Boolean, List<Person>> collect = personList.stream().collect(Collectors.groupingBy(x -> x.getSalary() > 5000));
    3. System.out.println(collect);
    4. // 根据性别分组
    5. Map<String, List<Person>> collect1 = personList.stream().collect(Collectors.groupingBy(Person::getGender));
    6. System.out.println(collect1);
    7. // 将员工根据性别分组,再按地区分组
    8. Map<String, Map<String, List<Person>>> collect2 = personList.stream()
    9. .collect(Collectors.groupingBy(Person::getGender, Collectors.groupingBy(Person::getArea)));
    10. System.out.println(collect2);

    筛选:

    1. // 筛选员工中工资大于8000的人,并形成新的集合
    2. List<Person> collect = personList
    3. .stream()
    4. .filter(item -> item.getSalary() > 5000)
    5. .collect(Collectors.toList());
    6. System.out.println(collect);

    映射:(将一个流的元素按照一定映射规则映射到另一个流中):

    1. // 将员工的工资全部增加1000
    2. // personList
    3. // .stream().map(item -> {
    4. // item.setSalary(item.getSalary() + 1000);
    5. // return item;
    6. // }).forEach(System.out::println);
    7. List collect = simpleList.stream().map(item -> {
    8. StringBuilder strb = new StringBuilder();
    9. strb.append(item);
    10. return strb;
    11. }).collect(Collectors.toList());
    12. System.out.println(collect);

    排序:sorted:

    1. // 将员工按工资由低到高排序(自然排序--升序)
    2. List<String> collect = personList.stream()
    3. .sorted(Comparator.comparing(Person::getSalary))
    4. .map(Person::getName)
    5. .collect(Collectors.toList());
    6. System.out.println(collect);
    7. // 按照员工工资的降序
    8. List<String> collect1 = personList
    9. .stream()
    10. .sorted(Comparator.comparing(Person::getSalary).reversed())
    11. .map(Person::getName)
    12. .collect(Collectors.toList());
    13. System.out.println(collect1);

    peek操作:调试:

    1. // 在stream中调试,stream不支持debug
    2. List<Person> collect = personList.stream()
    3. .filter(item -> item.getSalary() > 5000)
    4. .peek(System.out::println)
    5. .collect(Collectors.toList());
    6. System.out.println(collect);

     其他操作:合并、去重、限制、跳过。。。。

    1. /*
    2. distinct:去重
    3. skip:跳过几个数据
    4. limit:限制使用几个数据
    5. */
    6. simpleList
    7. .stream()
    8. .distinct()
    9. .skip(2)
    10. .limit(6)
    11. .forEach(System.out::println);

  • 相关阅读:
    人工智能申报!武汉市人工智能创新专项项目申报要求、流程和申报限制
    跨平台桌面完全体
    计组_cpu的结构和工作流程
    卷积神经网络 - 卷积神经网络的神经科学基础篇
    依赖示意图如何称之为模型
    硬件学习路线调研
    定制 or 订阅?未来中国 SaaS 行业发展趋势是什么?
    eventfd
    netty报错:too many open files,调整文件句柄
    【数据结构学习笔记】18:线段树(单点修改)
  • 原文地址:https://blog.csdn.net/weixin_49627122/article/details/126231075