java8特性快速对list集合的筛选过滤和计算

一、准备工作
1.创建一个Student对象

package com.shiro.test.java8特性;import java.io.Serializable;/*** 学生的实体类*/
public class Student implements Serializable {private String id;private String username;private Integer age;private String sex;private String status;//要加这个构造函数 为了给对象赋值public Student(String id, String username, int age, String sex,String status) {this.id = id;this.username = username;this.age = age;this.sex = sex;this.status = status;}public String getStatus() {return status;}public void setStatus(String status) {this.status = status;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}
}

2.创建一个测试类,创建list集合,向list中添加数据

@Testpublic void test2(){List<Student> studentList = new ArrayList<>();Student s1 = new Student("1","小张",20,"男","0");Student s2 = new Student("2","小李",22,"男","1");Student s3 = new Student("3","小花",21,"女","1");Student s4 = new Student("4","小华",18,"女","2");Student s5 = new Student("5","小流",28,"男","2");Student s6 = new Student("6","小吴",25,"男","0");Student s7 = new Student("7","小吴",25,"男","0");Student s8 = new Student("8","小吴",25,"男","0");studentList.add(s1);studentList.add(s2);studentList.add(s3);studentList.add(s4);studentList.add(s5);studentList.add(s6);studentList.add(s7);studentList.add(s8);

二、利用java8特性Stream流对list集合进行操作
1.利用stream流进行foreach遍历

studentList.stream.forEach(student ->{//处理逻辑 打印出所有学生的名单和年龄System.out.println(student.getUsername()+student.getAge());});


2.对studentList利用filter函数进行筛选 获取符合条件的

List<student> list = studentList.stream.filter(student ->Obgects.equals(studengt.getSex(),"女")).collect(Collectors.toList());
//list中存放的是性别是女的所有学生
//使用stream流进行foreach遍历
list.stream().forEach(student ->{System.out.println(student.getUsername()+student.getSex());});

3.对List集合进行去重

//将username相同的 进行去重
List<Student> unique = studentList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Student::getUsername))),ArrayList :: new));
unique.stream().forEach(student -> {System.out.println("-------------------"+student.getUsername());});

4.取出list集合对象中的某一个属性(取出list中的每一个对象的名字组成一个新的集合)

List<String> username=studentList.stream().map(Student::getUsername).collect(Collectors.toList());//不取重的List<String> username = studentList.stream(),map(Student::getUsername).distinct().collect(Collectors.toList());//这个是将名字取重之后的

5.过滤属性为空的字段(添加一条数据 名字为空的数据) //获取名字不为空的

  Student s9 = new Student("9","",25,"男","0");studentList.add(s9);List<String> IsEmptyUsernameList= studentList.stream().map(s-> s.getUsername()).filter(s-> !s.isEmpty()).collect(Collectors.toList());System.out.println(IsEmptyUsernameList);

6.根据其中的某一属性值进行计算
//(获取年龄的最大值、最小值、平均值、综合、个数)

IntSummaryStatistics resultNum =  studentList.stream().mapToInt((s) -> s.getAge()).summaryStatistics();//个数System.out.println(resultNum.getCount());//总大小System.out.println(resultNum.getSum());//最大值System.out.println(resultNum.getMax());//最小值System.out.println(resultNum.getMin());//平均值System.out.println(resultNum.getAverage());

java8特性快速对list集合的筛选过滤和计算相关推荐

  1. java对list筛选_java8特性快速对list集合的筛选过滤和计算

    java8特性快速对list集合的筛选过滤和计算 一.准备工作 1.创建一个Student对象 package com.shiro.test.java8特性; import java.io.Seria ...

  2. 简洁又快速地处理集合——Java8 Stream(下)

    上一篇文章我讲解 Stream 流的基本原理,以及它与集合的区别关系,讲了那么多抽象的,本篇文章我们开始实战,讲解流的各个方法以及各种操作 没有看过上篇文章的可以先点击进去学习一下 简洁又快速地处理集 ...

  3. java8新特性:对map集合排序

    一.简单介绍Map 在讲解Map排序之前,我们先来稍微了解下map,map是键值对的集合接口,它的实现类主要包括:HashMap, TreeMap, Hashtable以及LinkedHashMap等 ...

  4. Java8种Stream流相关操作——集合的筛选、归约、分组、聚合

    过滤.筛选   filter skip /*** 过滤 筛选*/@Testpublic void test2(){List<String> list = Arrays.asList(&qu ...

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

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

  6. java8新特性:对map集合排序,根据key或者value操作排序(升序、降序)

    java8新特性:对map集合排序,根据key或者value操作排序(升序.降序) 直接上代码: package com.drew.test; import java.util.List; impor ...

  7. Java面试准备(四)——Java8特性

    Java8特性 一.Lambda表达式 1. 理解函数式编程思想 2. 函数式接口(Functional Interface) 1)什么是函数式接口 2)常见的函数式接口 1. Supplier接口 ...

  8. Java8特性 stream流常用方法

    Java8特性 stream流常用方法 Java 8 API添加了一个新的抽象称为流Stream,可以让你以一种声明的方式处理数据. Stream 使用一种类似用 SQL 语句从数据库查询数据的直观方 ...

  9. 【Java】Java8特性官网学习之Stream的前世今生

    前言 最近看了<数据密集型应用系统设计>,间断介绍了 命令式语言和声明式语言的区别 UNIX的 设计哲学 MapReduce编程框架 数据流引擎 函数运算符(函数式编程) 能发现Java8 ...

最新文章

  1. swift中单例的创建及销毁
  2. jmeter 取json值_干货丨实战经验分析,带你走进Jmeter参数化
  3. 因xhost命令和DISPLAY环境变量操作不当导致无法启动Oracle图形化安装界面
  4. 2017-08-10 前端日报
  5. String 对象内存分配策略
  6. 网页爬虫中文乱码问题Python
  7. zbrush 添加纹理贴图_zbrush零基础新手必看入门讲解
  8. iOS获取ipa素材、提取ipa资源图片文件
  9. 官方文档太辣鸡?TensorFlow 2.0开源工具书,30天「无痛」上手
  10. jQuery之load方法
  11. mdb 查询过于复杂_【律联云知产课堂】南京商标查询主要从哪些方面判断一个商标是否适合注册?...
  12. 中国服务业发展的轨迹、逻辑与战略转变——改革开放40年来的经验分析
  13. 性能测试的基本流程【最新】
  14. gim-实时通讯框架
  15. MS Sql当中 money类型数据使用 Decimal 传输
  16. USB转232 转TTL概述
  17. 数字图像处理(第三版)
  18. Gym 100015A Another Rock-Paper-Scissors Problem
  19. 骨传导耳机听歌音质怎么样、公认音质好的骨传导耳机排名
  20. PHP简单MVC架构

热门文章

  1. 阿里巴巴Java开发手册一周年最终版
  2. JavaScript ——〖猜数字游戏〗10次机会
  3. PTA 7-3 小明的第一个扑克牌“魔术”(deque队列或链表操作)
  4. MySQL 获取当前时间
  5. 电脑技巧:分享六个非常实用的资源网站
  6. 一个命令导出局域网的IP和MAC
  7. shutdown命令用法
  8. 23种设计模式及应用场景
  9. word如何在文中添加参考文献的引用并自动更新
  10. asp期末大作业——在线考试系统网站