stream学习
Stream流
定义
Steam流,用于操作集合或者数组中的数据,大量结合了
Lamda
表达式的语法风格,代码简洁。重点: 流只能收集一次
获取Stream流
Stream流要与数据源建立连接。
1.list
直接调用
steam()
即可// list List<String> names = new ArrayList<>(); Collections.addAll(names, "品贵","红旗", "周芷若","张三丰"); Stream<String> stream = names.stream(); stream.filter(s -> s.contains("品")).forEach(s -> System.out.println(s));
2.set
直接调用
stream()
即可//set 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
需要先将
map
转换为entrySet()
,再通过entryset()
进行获取stream()
//map Map<String, Double> map = new HashMap<>(){{ put("古力娜扎", 172.3); put("迪丽热巴", 175.3); put("古力娜扎", 171.4); put("稀里哗啦", 189.3); put("巴啦啦小魔仙", 160.1); }}; // map.stream() Set<Map.Entry<String, Double>> entries = map.entrySet(); Stream<Map.Entry<String, Double>> kvs = entries.stream(); kvs.filter(e -> e.getKey().contains("巴")).forEach(System.out::println); // 简便写法 map.entrySet().stream().filter(e -> e.getKey().contains("巴")).forEach(System.out::println);
4.数组
使用
Arrays.stream()
使用
Stream.of()
//数组 String[] names2 = {"张翠山", "东方不败", "大唐不夜城", "回民街"}; Stream<String> s1 = Arrays.stream(names2); Stream<String> s2 = Stream.of(names2);
Steam流的常见中间方法
中间方法指的是调用完成后会返回新的Stream流,可以继续使用,支持链式编程。
package stream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class Test2 {
public static void main(String[] args) {
List<Double> scores = new ArrayList<>();
// 找出大于60分并升序排序后输出
Collections.addAll(scores, 88.5, 100.0, 60.6, 99.0, 9.5, 99.6, 25.9);
scores.stream().filter(s -> s >= 60).sorted().forEach(System.out::println);
System.out.println("-------------------------------------------");
List<Student> students = new ArrayList<>();
Student s1 = new Student("蜘蛛精", 26, 172.5);
Student s2 = new Student("蜘蛛精", 26, 174.5);
Student s3 = new Student("紫霞", 216, 122.5);
Student s4 = new Student("白晶晶", 25, 175.5);
Student s5 = new Student("牛魔王", 13, 166.5);
Collections.addAll(students, s1, s2, s3, s4, s5);
// 找出年龄大于等于23 年龄小于等于30 并且按照年龄输出
students.stream().filter(s -> s.getAge() >= 23 && s.getAge() <= 30).sorted((o1, o2) -> o2.getAge() - o1.getAge()).forEach(System.out::println);
System.out.println("-------------------------------------------");
// 取出身高前3名的学生 并输出
students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(), o1.getHeight())).limit(3).forEach(System.out::println);
System.out.println("-------------------------------------------");
// 取出身高倒数2名学生,并输出
students.stream().sorted((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).limit(2).forEach(System.out::println);
System.out.println("-------------------------------------------");
// 找出身高超过168的学生叫什么名字,要求去除重复的名字,再输出
students.stream().filter(s -> s.getHeight() > 168).map(Student::getName).distinct().forEach(System.out::println);
System.out.println("-------------------------------------------");
// 找出身高超过168的学生叫什么名字,要求去除重复的内容,再输出
// 首先需要重写equals和hashCode函数 让内容相同的对象设置为一样的
students.stream().filter(s -> s.getHeight() > 168).distinct().forEach(System.out::println);
System.out.println("-------------------------------------------");
Stream<String> st1 = Stream.of("张三", "李四");
Stream<String> st2 = Stream.of("张三2", "李四2", "王五");
Stream.concat(st1, st2).forEach(System.out::println);
}
}
Stream流常见的终结方法
package stream;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class Test3 {
public static void main(String[] args) {
List<Student> students = new ArrayList<>();
Student s1 = new Student("蜘蛛精", 26, 172.5);
Student s2 = new Student("蜘蛛精", 26, 172.5);
Student s3 = new Student("紫霞", 23, 167.6);
Student s4 = new Student("白晶晶", 25, 169.0);
Student s5 = new Student("牛魔王", 35, 183.8);
Student s6 = new Student("牛夫人", 34, 168.5);
Collections.addAll(students, s1, s2, s3, s4, s5, s6);
// 需求1:计算身高超过168的人数
System.out.println(students.stream().filter(s -> s.getHeight() >= 168).count());
//需求2:找出身高最高的学生对象并输出
System.out.println(students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get());
//需求3:找出身高最矮的学生对象并输出
System.out.println(students.stream().min(((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight()))).get());
//需求4:找出身高超过170的学生对象,放到一个新集合
List<Student> student1 = students.stream().filter(s -> s.getHeight() >= 170).collect(Collectors.toList());
System.out.println(student1);
Set<Student> student2 = students.stream().filter(s -> s.getHeight() >= 170).collect(Collectors.toSet());
System.out.println(student2);
//需求5:找出身高超过170的学生对象并把学生的名字和身高,存入到一个Map集合中
Map<String, Double> map = students.stream().filter(s -> s.getHeight() > 170).distinct()
.collect(Collectors.toMap(Student::getName, Student::getHeight));
map.forEach((k, v) -> {
System.out.println(k + "-----" + v);
});
// 收集到数组中
Object[] array = students.stream().filter(s -> s.getHeight() > 170).toArray();
System.out.println(Arrays.toString(array));
}
}