设计网站网站名称,2021最新域名没被封的,德化住房和城乡建设网站,wordpress角色什么是Stream#xff1f;
也叫Stream流#xff0c;是jdk8开始新增的一套API#xff0c;可以用来操作集合或者数组的数据
优势#xff1a;Stream流大量的结合了Lambda的语法风格来编程#xff0c;提供了一种更加强大#xff0c;更加简单的方式操作集合或数组中的数据
也叫Stream流是jdk8开始新增的一套API可以用来操作集合或者数组的数据
优势Stream流大量的结合了Lambda的语法风格来编程提供了一种更加强大更加简单的方式操作集合或数组中的数据代码更简洁可读性更好
import java.util.*;
import java.util.stream.Collectors;public class Work1 {public static void main(String[] args) {ListString names new ArrayList();names.add(温小辉);names.add(小朱);names.add(简隋英);names.add(邵群);names.add(小白);System.out.println(names);//找出带小的名字存入新的集合中去ListString list new ArrayList();for (String name : names) {if (name.contains(小)name.length()2){list.add(name);}}System.out.println(list);//开始使用Stream流List list1 names.stream().filter(s-s.contains(小)).filter(a-a.length()2).collect(Collectors.toList());System.out.println(list1);}
}
1如何获取集合和数组的Stream流 import java.util.*;
import java.util.stream.Stream;public class Work1 {public static void main(String[] args) {//1,如何获取List集合的Stream流ListString names new ArrayList();Collections.addAll(names,温小辉,小朱,简隋英,邵群,小白);StreamString stream names.stream();//2,如何获取Set集合的Stream流SetString set new HashSet();Collections.addAll(set,李玉,李程秀,兰波,白新羽,何故);StreamString stream1 set.stream();stream1.filter(s - s.contains(李)).forEach(s - System.out.println(s));//3,如何获取Map集合的Stream流?MapString,Doublemap new HashMap();map.put(娘娘腔,9.5);map.put(你却爱着一个SB,9.9);map.put(人鱼陷落,9.8);map.put(附加遗产,9.6);//键流final SetString keys map.keySet();final StreamString ks keys.stream();//值流final CollectionDouble values map.values();final StreamDouble vs values.stream();//键值对流final SetMap.EntryString, Double entries map.entrySet();final StreamMap.EntryString, Double kvs entries.stream();kvs.filter(e-e.getKey().contains(人)).forEach(e-System.out.println(e.getKey()--e.getValue()));String[] names2 {言逸,陆上锦,陆言,毕揽星,百刃};final StreamString s1 Arrays.stream(names2);final StreamString s2 Stream.of(names2);}
}
2Stream流的常用方法 import java.util.*;
import java.util.stream.Stream;public class Work1 {public static void main(String[] args) {//需求一找出成绩大于等于60分的数据并升序后再输出ListDouble scores new ArrayList();Collections.addAll(scores, 88.5,100.0,60.0,99.0,9.5,99.6,25.0);scores.stream().filter(s-s60).sorted().forEach(s- System.out.println(s));ListStudent students new ArrayList();Student s1 new Student(简隋英,32,188);Student s2 new Student(简隋英,32,188);Student s3 new Student(邵群,31,189);Student s4 new Student(洛羿,19,187);Student s5 new Student(李玉,21,188.5);Student s6 new Student(白新羽,22,185.2);Student s7 new Student(何故,25,183);Collections.addAll(students,s1,s2,s3,s4,s5,s6,s7);//找出年龄大于等于20且小于等于30并按照年龄降序输出students.stream().filter(s-s.getAge()20 s.getAge()30).sorted(((o1, o2) - o2.getAge()- o1.getAge())).forEach(s- System.out.println(s));System.out.println(.....................................);//取出身高最高的前三名students.stream().sorted(((o1, o2) - Double.compare(o2.getHeight(), -o1.getHeight()))).limit(3).forEach(s- System.out.println(s));System.out.println(......................................);//取出身高最矮的两名students.stream().sorted(((o1, o2) - Double.compare(o2.getHeight(), -o1.getHeight()))).skip(students.size()-2).forEach(s- System.out.println(s));//找出身高超过188的叫什么名字要求去掉重复的名字再输出//distinct去重复自定义类型的对象(希望内容一样就认为重复重写hashCode和equals方法)students.stream().filter(s-s.getHeight()188).map(s-s.getName()).distinct().forEach(s - System.out.println(s));final StreamString st1 Stream.of(张三, 王五);final StreamString st2 Stream.of(张三1, 王五2);final StreamObject allSt Stream.concat(st1, st2);allSt.forEach(s- System.out.println(s));}}
class Student{private String name;private int age;private double height;public Student() {}public Student(String name, int age, double height) {this.name name;this.age age;this.height height;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getAge() {return age;}public void setAge(int age) {this.age age;}public double getHeight() {return height;}public void setHeight(double height) {this.height height;}Overridepublic String toString() {return Student{ name name \ , age age , height height };}
}
3Stream流常见的终结方法 import java.util.*;
import java.util.stream.Collectors;public class Work1 {public static void main(String[] args) {ListStudent students new ArrayList();Student s1 new Student(简隋英,32,188);Student s2 new Student(邵群,31,189);Student s3 new Student(洛羿,19,187);Student s4 new Student(李玉,21,188.5);Student s5 new Student(白新羽,22,185.2);Student s6 new Student(何故,25,183);Collections.addAll(students,s1,s2,s3,s4,s5,s6);//找出身高超过188的并输出final long size students.stream().filter(s - s.getHeight() 188).count();System.out.println(size);//取出身高最高的并输出final Student s students.stream().max((o1, o2) - Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(s);//取出身高最矮的两名Student ss students.stream().min((o1, o2) - Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(ss);//找出身高超过185的并放到一个新集合中去返回List Student students1 students.stream().filter(a-a.getHeight()185).collect(Collectors.toList());System.out.println(students1);//找出身高超过185的并把对象的姓名和身高放到Map集合中去返回MapString,Double map students.stream().filter(a-a.getHeight()185).collect(Collectors.toMap(a-a.getName(),a-a.getHeight()));System.out.println(map);//放在数组中去Object[] arr students.stream().filter(a - a.getHeight() 185).toArray();System.out.println(Arrays.toString(arr));}
}