Stream流
1. 案例初体验
java
package cn.hdc.oop9.stream.using;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class t1 {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
LinkedList<String> l1 = new LinkedList<>();
list.add("wsn");
list.add("wzn");
list.add("wyr");
list.add("jsz");
list.add("zqy");
// 需求:把w开头的且长度为3的元素存储到一个新的集合
// 使用Stream前
list.forEach(item -> {
if (item.length() == 3 && item.charAt(0) == 'w') {
l1.add(item);
}
});
System.out.println(l1);
// 使用Stream后
List<String> l = list.stream()
.filter(item -> item.startsWith("w"))
.filter(item -> item.length() == 3)
.collect(Collectors.toList());
System.out.println(l);
}
}
2. 什么是Stream?
3. 使用步骤
3.1 获取Stream流
3.1.1 代码试用
java
package cn.hdc.oop9.stream.api;
import java.util.*;
import java.util.stream.Stream;
public class t1 {
public static void main(String[] args) {
//1、如何获取List集台的Stream流:
List<String> names = new ArrayList<>();
Collections.addAll(names, "张三丰", "张无忌", "周芷若", "赵敏", "张强");
Stream<String> s1 = names.stream();
//2、如何获取Set集合的stream流?
Set<String> set = new HashSet<>();
Collections.addAll(set, "刘德华", "张受玉", "蜘蛛精", "马德", "德玛西亚");
Stream<String> s2 = set.stream();
s2.filter(item -> item.contains("德")).forEach(item -> System.out.println(item));
//3、如何获取Map集合的stream流?
Map<String, Double> map = new HashMap<>();
map.put("古力娜扎", 172.3);
map.put("迪丽热巴", 168.3);
map.put("马尔扎哈", 166.3);
map.put("卡尔扎巴", 168.3);
Stream<String> s3 = map.keySet().stream();
Stream<Double> s4 = map.values().stream();
Stream<Map.Entry<String, Double>> s5 = map.entrySet().stream();
s5.filter(item -> item.getKey().contains("巴")).forEach(item -> System.out.println(item));
//4.如何获取数组的Stream流
String[] names2 = {"张翠山", "东方不败", "唐大山", "独孤求败"};
Arrays.stream(names2).filter(item -> item.contains("山")).forEach(item -> System.out.println(item));
Stream.of(names2).filter(item -> item.length() > 3).forEach(item -> System.out.println(item));
}
}
3.2 Stream中间方法
3.2.1 什么是中间方法
3.2.2 常见中间方法
3.2.3 代码试用
java
package cn.hdc.oop9.stream.api;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
public class t2 {
public static void main(String[] args) {
List<Double> scores = new ArrayList<>();
Collections.addAll(scores, 88.5, 100.0, 60.0, 99.0, 9.5, 99.6, 25.0);
//需求1:找出成绩大于等于60分的数据,并升序后,再输出。
scores.stream().filter(item -> item >= 60).sorted().forEach(item -> System.out.println(item));
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.3);
Student s6 = new Student("牛夫人", 34, 168.5);
Collections.addAll(students, s1, s2, s3, s4, s5, s6);
//需求2:找出年龄大于等于23,且年龄小于等于30岁的学生,并按照年龄降序输出
students.stream().filter(item -> item.getAge() >= 23 && item.getAge() <= 30).sorted((o1, o2) -> Integer.compare(o2.getAge(), o1.getAge())).forEach(item -> System.out.println(item));
System.out.println();
//需求3:取出身高最高的前3名学生,并输出。
students.stream().sorted((o1, o2) -> Double.compare(o2.getHeight(), o1.getHeight())).limit(3).forEach(item -> System.out.println(item));
System.out.println();
//需求4:取出身高倒数的2名学生,并输出。
students.stream().sorted((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).limit(2).forEach(item -> System.out.println(item));
System.out.println();
//需求5:找出身高超过168的学生叫什么名字,要求去除重复的名字,再输出。
students.stream().filter(item -> item.getHeight() > 168).map(item -> item.getName()).distinct().forEach(item -> System.out.println(item));
System.out.println();
//需求6:流合并
Stream<String> st1 = Stream.of("张三", "李四");
Stream<String> st2 = Stream.of("张三2", "李四2", "王五");
Stream.concat(st1, st2).forEach(System.out::println);
}
}
class Student {
private String name;
private int age;
private double height;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + 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;
}
}
3.3 Stream终结方法
3.3.1 什么是终结方法
3.3.2 常见的终结方法
3.3.3 代码试用
java
package cn.hdc.oop9.stream.api;
import java.util.*;
public class t3 {
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.3);
Student s6 = new Student("牛夫人", 34, 168.5);
Collections.addAll(students, s1, s2, s3, s4, s5, s6);
// 需求1:请计算出身高超过168的学生有几人。
long count = students.stream().filter(item -> item.getHeight() > 168).count();
System.out.println(count);
System.out.println();
// 需求2:请找出身高最高的学生对象,并输出。
Student max = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();
System.out.println(max);
// 需求3:请找出身高最矮的学生对象,并输出。
Student min = students.stream().min(Comparator.comparingDouble(Student::getHeight)).get();
System.out.println(min);
}
}
3.3.4 收集Stream流
3.3.5 代码试用
java
package cn.hdc.oop9.stream.api;
import java.util.*;
import java.util.stream.Collectors;
public class t3 {
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.3);
Student s6 = new Student("牛夫人", 34, 168.5);
Collections.addAll(students, s1, s2, s3, s4, s5, s6);
// 需求4:请找出身高超过170的学生对象,并放到一个新集合中去返回。
List<Student> l1 = students.stream().filter(item -> item.getHeight() > 170).collect(Collectors.toList());
Set<Student> l2 = students.stream().filter(item -> item.getHeight() > 170).collect(Collectors.toSet());
System.out.println(l1);
System.out.println(l2);
// 需求5:请找出身高超过170的学生对象,并把学生对象的名字和身高,存入到一个Map集合返回
Map<String, Double> map = students.stream().filter(item -> item.getHeight() > 170).distinct().collect(Collectors.toMap(item -> item.getName(), item -> item.getHeight()));
System.out.println(map);
// 需求6:请找出身高超过170的学生对象,并把学生对象存入到一个数组返回
Object[] arr = students.stream().filter(item -> item.getHeight() > 170).toArray();
Student[] arr1 = students.stream().filter(item -> item.getHeight() > 170).toArray(len -> new Student[len]);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr1));
}
}