Stream流
1、使用步骤
- 获取stream流
- 调用stream流的各种方法对数据进行处理、计算
- 获取处理的结果,遍历、统计、收集到一个新的集合中并且返回
一) 获取stream流的方法
package com.zpark.it.stream02;
import javax.lang.model.element.VariableElement;
import java.nio.channels.NonWritableChannelException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamDemo1 {
public static void main(String[] args) {
List<String> names= new ArrayList<>();
Collections.addAll(names,"张三丰","张无忌","张全蛋","小强");
Stream<String> stream = names.stream();
Set<String> set=new HashSet<>();
Collections.addAll(set,"李四","李五","张流");
Stream<String> stream1 = set.stream();
stream1.filter(s->s.startsWith("李")&&s.length()==2)
.forEach(s -> System.out.println(s));
Map<String,Double> map=new HashMap<>();
map.put("张德华",170.0);
map.put("张小风",182.0);
map.put("小二",80.0);
map.put("张小",188.0);
Set<String> keys = map.keySet();
Stream<String> ks = keys.stream();
Collection<Double> values = map.values();
Stream<Double> vs = values.stream();
Set<Map.Entry<String, Double>> entries = map.entrySet();
Stream<Map.Entry<String, Double>> kvs = entries.stream();
kvs.filter(e->e.getKey().contains("张")).forEach(e-> System.out.println(e.getKey()+"===>"+e.getValue()));
String[] names1={"张三","历史"};
Stream<String> s1 = Arrays.stream(names1);
Stream<String> s2 = Stream.of(names1);
}
}
二)Stream流常见的中间方法
package com.zpark.it.stream02;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class StreamDemo02 {
public static void main(String[] args) {
List<Double> scores = new ArrayList<>();
Collections.addAll(scores, 88.3, 100.3, 123.2, 99.3, 10.2);
scores.stream().filter(s -> s >= 60).sorted().forEach(s -> System.out.println(s));
List<Student> students = new ArrayList<>();
Student s1 = new Student("蜘蛛精", 26, 170.9);
Student s2 = new Student("蜘蛛精", 27, 172.9);
Student s3 = new Student("紫霞", 26, 171.9);
Student s4 = new Student("至尊宝", 23, 180.9);
Student s5 = new Student("牛魔王", 28, 182.9);
Collections.addAll(students, s1, s2, s3, s4, s5);
students.stream().filter(s -> s.getAge() >= 23 && s.getAge() <= 27)
.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));
students.stream().filter(s -> s.getHeight() > 168)
.map(s -> s.getName()).distinct().forEach(s -> System.out.println(s));
Stream<String> st1 = Stream.of("张三", "李四", "王五");
Stream<String> st2 = Stream.of("张三1", "李四2", "王五3");
Stream<String> all = Stream.concat(st1, st2);
all.forEach(System.out::println);
}
}
}
三)Stream流常见的终结方法
package com.zpark.it.stream02;
import java.net.SocketTimeoutException;
import java.util.*;
import java.util.stream.Collectors;
public class StreamDemo03 {
public static void main(String[] args) {
List<Student> students=new ArrayList<>();
Student s1 = new Student("蜘蛛精", 26, 170.9);
Student s2 = new Student("蜘蛛精", 27, 172.9);
Student s3 = new Student("紫霞", 26, 168.9);
Student s4 = new Student("至尊宝", 23, 180.9);
Student s5 = new Student("牛魔王", 28, 182.9);
Collections.addAll(students, s1, s2, s3, s4, s5);
long count = students.stream().filter(s -> s.getHeight() > 168).count();
System.out.println(count);
Student student = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
System.out.println(student);
Student student1 = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
System.out.println(student1);
System.out.println("------------------------");
List<Student> students2 = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toList());
System.out.println(students2);
System.out.println("+++++++++++++++++++++");
Set<Student> students3 = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toSet());
System.out.println(students3);
System.out.println("--------++++++++++++++++++++++");
Map<String, Double> map = students.stream().filter(s -> s.getHeight() > 170)
.distinct().collect(Collectors.toMap(s -> s.getName(), s -> s.getHeight()));
System.out.println(map);
Student[] arr = students.stream().filter(s -> s.getHeight() < 170).toArray(len->new Student[len]);
System.out.println(Arrays.toString(arr));
}
}