什么是Stream?
也叫Stream流,是jdk8开始新增的一套API,可以用来操作集合或者数组的数据
优势:Stream流大量的结合了Lambda的语法风格来编程,提供了一种更加强大,更加简单的方式操作集合或数组中的数据,代码更简洁,可读性更好
import java.util.*;
import java.util.stream.Collectors;
public class Work1 {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("温小辉");
names.add("小朱");
names.add("简隋英");
names.add("邵群");
names.add("小白");
System.out.println(names);
//找出带小的名字,存入新的集合中去
List<String> 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流?
List<String> names = new ArrayList<>();
Collections.addAll(names,"温小辉","小朱","简隋英","邵群","小白");
Stream<String> stream = names.stream();
//2,如何获取Set集合的Stream流?
Set<String> set = new HashSet<>();
Collections.addAll(set,"李玉","李程秀","兰波","白新羽","何故");
Stream<String> stream1 = set.stream();
stream1.filter(s -> s.contains("李")).forEach(s -> System.out.println(s));
//3,如何获取Map集合的Stream流?
Map<String,Double>map = new HashMap<>();
map.put("娘娘腔",9.5);
map.put("你却爱着一个SB",9.9);
map.put("人鱼陷落",9.8);
map.put("附加遗产",9.6);
//键流
final Set<String> keys = map.keySet();
final Stream<String> ks = keys.stream();
//值流
final Collection<Double> values = map.values();
final Stream<Double> vs = values.stream();
//键值对流
final Set<Map.Entry<String, Double>> entries = map.entrySet();
final Stream<Map.Entry<String, Double>> kvs = entries.stream();
kvs.filter(e->e.getKey().contains("人")).forEach(e->System.out.println(e.getKey()+"-->"+e.getValue()));
String[] names2 = {"言逸","陆上锦","陆言","毕揽星","百刃"};
final Stream<String> s1 = Arrays.stream(names2);
final Stream<String> s2 = Stream.of(names2);
}
}
2,Stream流的常用方法
import java.util.*;
import java.util.stream.Stream;
public class Work1 {
public static void main(String[] args) {
//需求一:找出成绩大于等于60分的数据,并升序后,再输出
List<Double> scores = new ArrayList<>();
Collections.addAll(scores, 88.5,100.0,60.0,99.0,9.5,99.6,25.0);
scores.stream().filter(s->s>=60).sorted().forEach(s-> System.out.println(s));
List<Student> 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 Stream<String> st1 = Stream.of("张三", "王五");
final Stream<String> st2 = Stream.of("张三1", "王五2");
final Stream<Object> 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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
}
3,Stream流常见的终结方法
import java.util.*;
import java.util.stream.Collectors;
public class Work1 {
public static void main(String[] args) {
List<Student> 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集合中去返回
Map<String,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));
}
}