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

article/2025/7/28 7:10:13

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

http://chatgpt.dhexx.cn/article/DR6HSaJN.shtml

相关文章

Java8特性大全(最新版)

一、序言 Java8 是一个里程碑式的版本&#xff0c;凭借如下新特性&#xff0c;让人对其赞不绝口。 Lambda 表达式给代码构建带来了全新的风格和能力&#xff1b;Steam API 丰富了集合操作&#xff0c;拓展了集合的能力&#xff1b;新日期时间 API 千呼万唤始出来&#xff1b;…

Java 8特性之Optional详解

一、Optional类 简介 Optional类是 Java 8 引入的一个很有趣的特性。它主要解决的问题是臭名昭著的空指针异常&#xff08;NullPointerException&#xff09; Optional 类是一个可以为null的容器对象。如果值存在则isPresent()方法会返回true&#xff0c;调用get()方法会返回…

Java基础之java8 新特性

Java基础之java8 新特性 一、Lambda 表达式二、Stream 初体验三、方法引用四、默认方法五、Optional 类六、Base 64七、字符串拼接八、 equals 与 instanceof九、final 一、Lambda 表达式 Lambda 表达式是在 Java8 中引入的&#xff0c;并号称是 Java8 的最大的特点。Lambda 表…

java8新特性Lambda和Stream以及函数式接口等新特性介绍

主要内容 1.Lambda 表达式 2.函数式接口 3.方法引用与构造器引用 4.Stream API 5.接口中的默认方法与静态方法 6.新时间日期API 7.其他新特性 Java 8新特性简介 速度更快速度更快代码更少&#xff08;增加了新的语法Lambda 表达式&#xff09;强大的Stream API便于并行最大化…

python 读取并显示图片的两种方法

转自&#xff1a;http://www.cnblogs.com/yinxiangnan-charles/p/5928689.html 在 python 中除了用 opencv&#xff0c;也可以用 matplotlib 和 PIL 这两个库操作图片。本人偏爱 matpoltlib&#xff0c;因为它的语法更像 matlab。 一、matplotlib 1. 显示图片 import matplotli…

Python同时显示多张图片在一个画面中(两种方法)

很多时候需要把很多图片同时显示到一个画面中&#xff0c;现在分享两个方法&#xff0c;这里我恰好拿之前写的爬取网上图片保存到本地的爬虫模型爬一些图片作为素材Python 爬虫批量爬取网页图片保存到本地。 得到素材如下所示&#xff1a; 现在让这些图片同时显示。 方法一 …

python 打开 显示图片

import matplotlib.pyplot as plt # plt 用于显示图片 from PIL import Imageplt.figure() plt.plot([1,2], [1,2])plt.rcParams[font.sans-serif] [SimHei] # 中文乱码 plt.imshow(Image.open("b.png"))plt.axis(off)plt.tight_layout() manager plt.get_current…

python 把matplotlib绘制的图片显示到html中

需求 一般网页中图片显示是给url链接过去&#xff0c;但是有的时候显示的图表是临时计算绘制的&#xff0c;并不需要保存&#xff0c;因此就需要直接显示一个图片的方法。 灵感是来自于jupyter&#xff0c;发现他是这样的&#xff1a; 估计是base64编码了。 查了一下如何把ma…

python如何将图片显示在网页上

from flask import Flask, render_template_string import base64 import cv2import osapp Flask(__name__)# 读取图像app.route(/)def index():# 读取图像文件并将其转换为Base64编码的字符串img_path 1.pngimg_data open(img_path, rb).read()img_base64 base64.b64encod…

python matplotlib 显示图像

python matplotlib 显示图像 首先需要import import os from PIL import Image import matplotlib.pyplot as plt 显示一幅彩色图片 img Image.open(os.path.join(images, 2007_000648 .jpg))plt.figure("Image") # 图像窗口名称 plt.imshow(img) plt.axis(on)…

python中plt.imshow()不显示图片

python画图函数可能是需要一些设置&#xff0c;或者一些特定的函数运行才会出现图片的。 Python中plt.imshow(image)无法显示图片解决办法 使用plt.imshow()发现不能显示图片&#xff0c;加了plt.show()也还是不能显示 先引入包pylab import pylab 然后在plt.imshow(img)后面…

Python tkinter之PhotoImage图片显示问题

作为Python小白的你&#xff0c;有没有遇到过这几个问题&#xff1f; PhotoImage不支持jpg等图片格式&#xff1b;将PhotoImage放在函数里图片不显示&#xff1b;循环加入图片只显示一张&#xff1b; 这些难倒了一大批小白&#xff0c;那么接下来&#xff0c;本文教你如何解决…

python图显示在新的窗口_python在新的图片窗口显示图片(图像)的方法

python在新的图片窗口显示图片(图像)的方法 使用python画图,发现生成的图片在console里。不仅感觉很别扭,很多功能也没法实现(比如希望在一幅图里画两条曲线)。 想像matlab一样单独地生成一个图片窗口,然后我在网上找了一个多小时,都没有找到想要的,要么仅仅是画图的教…

python人工智能-动态显示图片的几种办法实现

python人工智能动态显示图片 任务简介&#xff0c;在电脑创建一个文件加&#xff0c;利用代码往文件加入照片文件&#xff0c; 然后动态显示该文件夹中的照片&#xff0c;显示给用户观看&#xff0c;可以利用open-cv&#xff0c;matplotlib,PIL等技术实现 这里是电脑一个装有照…

Python 3 显示图像的方法

如果你正在寻找一个库来帮助你在 Python 中显示图像&#xff0c;那么你很幸运。Python 有许多不同的库可用于显示图像。三个最受欢迎的库是Pickel、Matplotlib 和 OpenCV。 到底应该选哪一个&#xff0c;还是看自己的个人喜好了。我喜欢并使用 OpenCV 而不是 Pillow 和 Matplot…

python显示图片_python图像显示

广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 使用python进行数字图片处理,还得安装pillow包。 虽然python里面自带一个pil(python images library), 但这个库现在已经停止更新了,所以使用pillow, 它是由p…

python显示图片

python显示图片两种方法 1.利用Pillow包 未安装的可以通过pip install Pillow安装 from PIL import Image img Image.open(notMNIST_large/A/aGFua28udHRm.png) img.show() 显示的图片如下&#xff1a; 2.利用matplotlib 同样可以通过pip进行安装 from PIL import Image …

使用Python查看并显示图像

今天又来写一个Python的基础用法&#xff0c;使用Python来显示图像&#xff0c;不多说&#xff0c;代码如下&#xff1a; import matplotlib.pyplot as plt from matplotlib.image import imreadimg imread(C:/Users/xx/Desktop/xx.jpg) plt.imshow(img)plt.show() 2022.05.1…

基于python多光谱遥感数据处理、图像分类、定量评估及机器学习方法应用

普通数码相机记录了红、绿、蓝三种波长的光&#xff0c;多光谱成像技术除了记录这三种波长光之外&#xff0c;还可以记录其他波长&#xff08;例如&#xff1a;近红外、热红外等&#xff09;光的信息。与昂贵、不易获取的高光谱、高空间分辨率卫星数据相比&#xff0c;中等分辨…

推扫式和快照式高光谱成像系统在红肉掺假检测中的应用

目录 摘要 推扫式高光谱成像系统的应用 快照式高光谱成像系统的应用 总结 参考文献 摘要 肉类掺假常见于用低价肉代替高价肉获取非法利益&#xff0c;它不但影响了食品的质量&#xff0c;甚至会威胁到人们的健康。目前的检测手段一般是取样后送样检测&#xff0c;很难实时…