本博客系转载,原文链接:https://blog.csdn.net/baidu_38083619/article/details/87891206
一个例子:

public static void main(String[] args) {List<Student> list = Lists.newArrayList();list.add(new Student("测试", "男", 18));list.add(new Student("开发", "男", 20));list.add(new Student("运维", "女", 19));list.add(new Student("DBA", "女", 22));list.add(new Student("运营", "男", 24));list.add(new Student("产品", "女", 21));list.add(new Student("经理", "女", 25));list.add(new Student("产品", "女", 21));//求性别为男的学生集合List<Student> l1 = list.stream().filter(student -> student.sex.equals("男")).collect(toList());//map的key值true为男,false为女的集合Map<Boolean, List<Student>> map = list.stream().collect(partitioningBy(student -> student.getSex().equals("男")));//求性别为男的学生总岁数Integer sum = list.stream().filter(student -> student.sex.equals("男")).mapToInt(Student::getAge).sum();//按性别进行分组统计人数Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.summingInt(p -> 1)));//判断是否有年龄大于25岁的学生boolean check = list.stream().anyMatch(student -> student.getAge() > 25);//获取所有学生的姓名集合List<String> l2 = list.stream().map(Student::getName).collect(toList());//求所有人的平均年龄double avg = list.stream().collect(averagingInt(Student::getAge));//求年龄最大的学生Student s = list.stream().reduce((student, student2) -> student.getAge() > student2.getAge() ? student:student2).get();Student stu = list.stream().collect(maxBy(Comparator.comparing(Student::getAge))).get();//按照年龄从小到大排序List<Student> l3 = list.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(toList());//求年龄最小的两个学生List<Student> l4 = l3.stream().limit(2).collect(toList());//获取所有的名字,组成一条语句String str = list.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]"));//获取年龄的最大值、最小值、平均值、求和等等IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(Student::getAge).summaryStatistics();System.out.println(intSummaryStatistics.getMax());System.out.println(intSummaryStatistics.getCount());}@Data@AllArgsConstructorstatic class Student{String name;String sex;Integer age;}

最后给大家说一个并行化流操作parallelStream(),跟stream()的用法一样。使用parallelStream就能立即获得一个拥有并行能力的流,利用好并行化非常重要。不过并不是所有的流计算都要使用并行化流操作,只有当计算机是多核并且集合数据量较大(超过一万)的时候使用并行化流操作可以提高效率。

影响性能的五要素:数据大小、源数据结构、值是否装箱、可用的CPU数量以及处理每个元素所花的时间。
关于利用stream求和的一个例子,可以学习下这种用法:

package lambda;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class StreamTest {public static void main(String[] args) {List<List<String>> finalList = new ArrayList<>();finalList.add(new ArrayList<>(Arrays.asList("1", "2", "3", "4")));finalList.add(new ArrayList<>(Arrays.asList("5", "6", "7", "8")));finalList.add(new ArrayList<>(Arrays.asList("9", "10", "11", "12")));int checkCount1 = finalList.stream()
//                .map(l -> l.size()).map(List::size).reduce(0, (res, n) -> res + n); // 后面可以加.intValue()System.out.println("checkCount1: " + checkCount1);int checkCount2 = finalList.stream()
//                .mapToInt(l -> l.size()).mapToInt(List::size).sum();System.out.println("checkCount2: " + checkCount2);}
}

Java8之list.stream的常见使用例子相关推荐

  1. Java8新特性Stream的常见用法

    目录 Stream简介 Stream的使用 创建流 (1) 通过Stream.of()将元素转化成流 (2)每个集合都可以通过调用 stream() 方法来产生一个流 使用举例 (1) 遍历/匹配(f ...

  2. Java8 Stream:20+实际例子,玩转集合的筛选、归约、分组、聚合

    来源: https://blog.csdn.net/mu_wind/article/details/109516995 Java8中的stream,可大幅提升咱们的开发效率,带大家看下stream到底 ...

  3. Java8新特性Stream流详解

    陈老老老板 说明:新的专栏,本专栏专门讲Java8新特性,把平时遇到的问题与Java8的写法进行总结,需要注意的地方都标红了,一起加油. 本文是介绍Java8新特性Stream流常用方法超详细教学 说 ...

  4. Java8中Lambda表达式的10个例子

    Java8中Lambda表达式的10个例子  例1 用Lambda表达式实现Runnable接口 Java代码   //Before Java 8: new Thread(new Runnable() ...

  5. Java8中的Stream

    Java8 Stream是一个非常好用的工具,结合Lambda表达式,可以非常方便的来操作各种集合. 文章目录 Stream知识图谱 Stream概述 Stream的创建 Stream的使用 遍历/匹 ...

  6. java8中的Stream用法详解

    项目github地址:bitcarmanlee easy-algorithm-interview-and-practice 欢迎大家star,留言,一起学习进步 1.为什么java8中加入Stream ...

  7. 【Java8新特性】面试官问我:Java8中创建Stream流有哪几种方式?

    写在前面 先说点题外话:不少读者工作几年后,仍然在使用Java7之前版本的方法,对于Java8版本的新特性,甚至是Java7的新特性几乎没有接触过.真心想对这些读者说:你真的需要了解下Java8甚至以 ...

  8. java8 lambda表达式Stream对List常用操作总结

    List最为java编程语音使用最频繁的数据结构之一,经常涉及到对List数据的各种处理,以前我们只能通过遍历的方式,自己去逐条处理,java8提供了Stream能够满足大部分日常对List的操作,如 ...

  9. java8新特性-stream对map集合进行过滤的方法

    java8新特性-stream对map集合进行过滤的方法 stream对map集合进行过滤的方法

最新文章

  1. iis php5.3 mysql_Win2008 R2配置IIS7.5+PHP Manager+PHP5.3+Mysql5.5+Wincache
  2. 从源码透析gRPC调用原理
  3. Vue使用全局样式,页面没有发生变化:逗号是中文的,引起错误,样式不变化 也没有报错就是不起作用
  4. word打开文档很久很慢_word文档打开特别慢怎么解决,word10打开文档很慢
  5. PHP 基础篇 - PHP 中 DES 加解密详解
  6. java编译_解析 Java 即时编译器原理。
  7. 《数学建模:基于R》——1.1 数据的描述性分析
  8. 第2章 数据库系统体系结构
  9. configure: error: Cannot find the WebServer
  10. 数组-滑动窗口(直接套模板完事儿)
  11. NRF52840 NRF52833 nRF5 SDK 开发
  12. python1~10阶乘_阶乘python怎么打
  13. HDU5855(最大权闭合图构图技巧+裸的最大流)
  14. 猫哥教你写爬虫 008--input()函数
  15. 如何让图片变成全景图,vr全景图怎么拍摄和制作
  16. 为第九大股东;此前40次增持民生银行A股股份
  17. 快进来看王冰冰!青年大学习提醒系统来了!!
  18. 【妙python】按照元素长度排序列表
  19. 37.深度解密三十七:网络营销推广之百度经验营销全过程步骤讲解
  20. 基于vue实现精妙绝伦的三级联动

热门文章

  1. WebResource.axd引起的问题
  2. 文件的I/O c++
  3. 写科研论文的高级方法学
  4. xbox360 ubuntu14.04连接问题
  5. Linux CGLIB升级,cglib升级建议
  6. java junit 运行_运行Junit方法项目启动不了
  7. java ftp连接成功 上传失败_ftp自动上传工具,如何设置及配置ftp自动上传工具
  8. 你真的会php,你真的会PHP吗?
  9. crontab 示例_Crontab示例–每5分钟
  10. easymock使用方法_EasyMock最终方法– PowerMock,JUnit 4,TestNG