东营网站建设铭盛信息,第一媒体app最新版本,厦门seo招聘,永仁县工程建设信息网站Stream初相识 概括讲#xff0c;可以将Stream流操作分为3种类型#xff1a;
创建Stream Stream中间处理 终止Steam 每个Stream管道操作类型都包含若干API方法#xff0c;先列举下各个API方法的功能介绍。
开始管道 主要负责新建一个Stream流#xff0c;或者基于现有的数组…Stream初相识 概括讲可以将Stream流操作分为3种类型
创建Stream Stream中间处理 终止Steam 每个Stream管道操作类型都包含若干API方法先列举下各个API方法的功能介绍。
开始管道 主要负责新建一个Stream流或者基于现有的数组、List、Set、Map等集合类型对象创建出新的Stream流。 API 功能说明 stream() 创建出一个新的stream串行流对象 parallelStream() 创建出一个可并行执行的stream流对象 Stream.of() 通过给定的一系列元素创建一个新的Stream串行流对象 中间管道 负责对Stream进行处理操作并返回一个新的Stream对象中间管道操作可以进行叠加。 API 功能说明 filter() 按照条件过滤符合要求的元素 返回新的stream流 map() 将已有元素转换为另一个对象类型一对一逻辑返回新的stream流 flatMap() 将已有元素转换为另一个对象类型一对多逻辑即原来一个元素对象可能会转换为1个或者多个新类型的元素返回新的stream流 limit() 仅保留集合前面指定个数的元素返回新的stream流 skip() 跳过集合前面指定个数的元素返回新的stream流 concat() 将两个流的数据合并起来为1个新的流返回新的stream流 distinct() 对Stream中所有元素进行去重返回新的stream流 sorted() 对stream中所有的元素按照指定规则进行排序返回新的stream流 peek() 对stream流中的每个元素进行逐个遍历处理返回处理后的stream流 终止管道 顾名思义通过终止管道操作之后Stream流将会结束最后可能会执行某些逻辑处理或者是按照要求返回某些执行后的结果数据 API 功能说明 count()返回stream处理后最终的元素个数 max()返回stream处理后的元素最大值 min()返回stream处理后的元素最小值 findFirst()找到第一个符合条件的元素时则终止流处理 findAny()找到任何一个符合条件的元素时则退出流处理这个对于串行流时与findFirst相同对于并行流时比较高效任何分片中找到都会终止后续计算逻辑 anyMatch()返回一个boolean值类似于isContains(),用于判断是否有符合条件的元素 allMatch()返回一个boolean值用于判断是否所有元素都符合条件 noneMatch()返回一个boolean值 用于判断是否所有元素都不符合条件 collect()将流转换为指定的类型通过Collectors进行指定 toArray()将流转换为数组 iterator()将流转换为Iterator对象 foreach()无返回值对元素进行逐个遍历然后执行给定的处理逻辑 import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;Data
Builder
AllArgsConstructor
NoArgsConstructor
public class StudentInfo implements ComparableStudentInfo {//姓名private String name;//性别 true男 false女private Boolean sex;//年龄private Integer age;//身高单位米private Double height;//出生日期private LocalDate birthday;Overridepublic int compareTo(StudentInfo student) {return this.age.compareTo(student.getAge());}Overridepublic String toString() {return String.format(%s\t\t%s\t\t%s\t\t%s\t\t%s,this.name, this.sex.toString(), this.age.toString(), this.height.toString(), birthday.toString());}/*** 打印学生信息的静态方法** param studentList 学生信息列表*/public static void printStudentList(ListStudentInfo studentList) {System.out.println(【姓名】\t\t【性别】\t\t【年龄】\t\t【身高】\t\t【生日】);System.out.println(-----------------------------------------------------);studentList.forEach(s - System.out.println(s.toString()));System.out.println( );}}class testmain{public static void main(String[] args) {ListStudentInfo studentList new ArrayList();//使用builder添加数据studentList.add(StudentInfo.builder().name(李四明).sex(true).age(18).height(1.88).birthday( LocalDate.of(2007, 8, 30)).build());// 添加测试数据请不要纠结数据的严谨性studentList.add(new StudentInfo(王五明, true, 23, 1.89, LocalDate.of(2000, 1, 16)));studentList.add(new StudentInfo(麻六的, false, 27, 1.75, LocalDate.of(1996, 9, 20)));studentList.add(new StudentInfo(刘七已, false, 30, 1.93, LocalDate.of(1993, 6, 19)));studentList.add(new StudentInfo(王八爬, false, 30, 1.75, LocalDate.of(1993, 6, 19)));// 输出没有过滤条件的学生列表StudentInfo.printStudentList(studentList);// 添加过滤条件比如年龄小于25岁并且身高大于1米7的学生列表ListStudentInfo ageHeightList studentList.stream().filter(student - student.getAge() 25 student.getHeight() 1.7).collect(Collectors.toList());// 输出符合条件的学生列表StudentInfo.printStudentList(ageHeightList);//输出listStudentInfo.printStudentList(studentList);//从对象中提取age。并过滤年龄ListInteger nameListstudentList.stream().map(StudentInfo::getAge).filter(f-f18).distinct().collect(Collectors.toList());nameList.forEach(System.out::println);}
} 文章转载自: http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.hqxyt.cn.gov.cn.hqxyt.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.mingjiangds.com.gov.cn.mingjiangds.com http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.xqndf.cn.gov.cn.xqndf.cn http://www.morning.ywndg.cn.gov.cn.ywndg.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn http://www.morning.amlutsp.cn.gov.cn.amlutsp.cn http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.mksny.cn.gov.cn.mksny.cn http://www.morning.plpqf.cn.gov.cn.plpqf.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.nckjk.cn.gov.cn.nckjk.cn http://www.morning.tqsmc.cn.gov.cn.tqsmc.cn http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.mhnr.cn.gov.cn.mhnr.cn http://www.morning.stbfy.cn.gov.cn.stbfy.cn http://www.morning.kqblk.cn.gov.cn.kqblk.cn http://www.morning.fhwfk.cn.gov.cn.fhwfk.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn http://www.morning.pctsq.cn.gov.cn.pctsq.cn http://www.morning.nclps.cn.gov.cn.nclps.cn http://www.morning.qfcnp.cn.gov.cn.qfcnp.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.wchsx.cn.gov.cn.wchsx.cn http://www.morning.rqgjr.cn.gov.cn.rqgjr.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.attorneysportorange.com.gov.cn.attorneysportorange.com http://www.morning.jqkjr.cn.gov.cn.jqkjr.cn http://www.morning.ymmjx.cn.gov.cn.ymmjx.cn http://www.morning.tkzrh.cn.gov.cn.tkzrh.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.qhtlq.cn.gov.cn.qhtlq.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.bbrf.cn.gov.cn.bbrf.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.xnymt.cn.gov.cn.xnymt.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.gllhx.cn.gov.cn.gllhx.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.rxwfg.cn.gov.cn.rxwfg.cn http://www.morning.jksgy.cn.gov.cn.jksgy.cn http://www.morning.tsdqr.cn.gov.cn.tsdqr.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.shxrn.cn.gov.cn.shxrn.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.rwbh.cn.gov.cn.rwbh.cn http://www.morning.rxydr.cn.gov.cn.rxydr.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.qxmys.cn.gov.cn.qxmys.cn http://www.morning.ktsth.cn.gov.cn.ktsth.cn http://www.morning.lnrr.cn.gov.cn.lnrr.cn http://www.morning.qlry.cn.gov.cn.qlry.cn http://www.morning.xphls.cn.gov.cn.xphls.cn http://www.morning.mngyb.cn.gov.cn.mngyb.cn http://www.morning.nytqy.cn.gov.cn.nytqy.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn