本博客系转载:https://zhuanlan.zhihu.com/p/97493325
1.Stream语法讲解
Stream执行流程很简单,主要有三个,首先创建一个Stream,然后使用Stream操作数据,最后终止Stream。有点类似于Stream的生命周期。下面我们根据其流程来一个一个讲解。
前提准备,首先我们创建一个Student类,以后我们每次都是操作这个类。
public class Student {
private Integer id;
private String name;
private Integer age;
private Double score;
public Student() {
}
public Student(Integer id, String name, Integer age, Double score) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
//getter和setter方法
//toString方法
}
2.创建一个Stream
2.1.通过一个集合创建Stream
@Test
public void test1(){
List<Student> studentList = StudentData.getStudents();
//第一种:返回一个顺序流
Stream<Student> stream = studentList.stream();
//第二种:返回一个并行流
Stream<Student> stream2 = studentList.parallelStream();
}
2.2.通过一个数组创建Stream
@Test
public void test2(){
//获取一个整形Stream
int[] arr = new int[]{1,2,34,4,65,7,87,};
IntStream intStream = Arrays.stream(arr);
//获取一个Student对象Stream
Student[] students = StudentData.getArrStudents();
Stream<Student> stream = Arrays.stream(students);
}
2.3.通过Stream.of
@Test
public void test3(){
Stream<Integer> integerStream = Stream.of(1, 2, 3, 5, 6, 7, 8);
Stream<String> stringStream = Stream.of("1", "2", "3", "4", "5");
Stream<Student> studentStream = Stream.of(
new Student(1, "刘备", 18, 90.4),
new Student(2, "张飞", 19, 87.4),
new Student(3, "关羽", 21, 67.4));
}
2.4.创建一个无限流
@Test
public void test4(){
//每隔5个数取一个,从0开始,此时就会无限循环
Stream.iterate(0,t->t+5).forEach(System.out::println);
//每隔5个数取一个,从0开始,只取前5个数
Stream.iterate(0,t->t+5).limit(5).forEach(System.out::println);
//取出一个随机数
Stream.generate(Math::random).limit(5).forEach(System.out::println);
}
3.使用Stream操作数据
3.1.筛选和切片
@Test
public void test1(){
List<Student> list = StudentData.getStudents();
//(1)过滤:过滤出所有年龄大于20岁的同学
list.stream().filter(item->item.getAge()>20).forEach(System.out::println);
//(2)截断流:筛选出前3条数据
list.stream().limit(3).forEach(System.out::println);
//(3)跳过元素:跳过前5个元素
list.stream().skip(5).forEach(System.out::println);
//(4)过滤重复数据:
list.stream().distinct().forEach(System.out::println);
}
3.2.映射
@Test
public void test2(){
//(1)map操作
List<String> list = Arrays.asList("java","python","go");
Stream<String> stream = list.stream();
//此时每一个小写字母都有一个大写的映射
stream.map(str -> str.toUpperCase()).forEach(System.out::println);
//筛选出所有的年龄,再过滤出所有大于20的年龄有哪些
List<Student> studentList = StudentData.getStudents();
Stream<Student> stream1 = studentList.stream();
Stream<Integer> ageStream = stream1.map(Student::getAge);
ageStream.filter(age->age>20).forEach(System.out::println);
//(2)floatMap:将流中的每一个值换成另外一个流
}
3.3.排序
public void test3(){
//(1)自然排序
List<Integer> list = Arrays.asList(4,3,7,9,12,8,10,23,2);
Stream<Integer> stream = list.stream();
stream.sorted().forEach(System.out::println);
//(2)对象排序:对象类可以先实现comparable接口,或者是直接指定
//第一种:先实现compable接口
List<Student> studentList = StudentData.getStudents();
studentList.stream().sorted().forEach(System.out::println);
//第二种:直接指定comparable
List<Student> studentList1 = StudentData.getStudents();
studentList1.stream()
.sorted((e1,e2)-> Integer.compare(e1.getAge(),e2.getAge()))
.forEach(System.out::println);
}
4.终止Stream
4.1.匹配和查找
public void test1(){
List<Student> list = StudentData.getStudents();
//(1)判断所有的学生年龄是否都大于20岁
boolean allMatch = list.stream().allMatch(item -> item.getAge() > 20);
//(2)判断是否存在学生的年龄大于20岁
boolean anyMatch = list.stream().anyMatch(item -> item.getAge() > 20);
//(3)判断是否存在学生叫曹操
boolean noneMatch = list.stream().noneMatch(item -> item.getName().equals("曹操"));
//(4)查找第一个学生
Optional<Student> first = list.stream().findFirst();
//(5)查找所有的学生数量
long count = list.stream().count();
long count1 = list.stream().filter(item -> item.getScore() > 90.0).count();
//(6)查找当前流中的元素
Optional<Student> any = list.stream().findAny();
//(7)查找学生最高的分数:Student实现了comparable接口的话,可直接比较
Stream<Double> doubleStream = list.stream().map(item -> item.getScore());
doubleStream.max(Double::compare);
//(8)查找学生最低的分数
}
4.2.归约
public void test2(){
//(1)计算数的总和
List<Integer> list = Arrays.asList(1,2,3,4,5);
list.stream().reduce(0,Integer::sum);
//(3)计算学生总分
List<Student> studentList = StudentData.getStudents();
Stream<Double> doubleStream = studentList.stream().map(Student::getScore);
doubleStream.reduce(Double::sum);
}
4.3.收集
public void test3(){
List<Student> studentList = StudentData.getStudents();
//返回一个list
List<Student> listStream = studentList.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toList());
//返回一个Set
Set<Student> setStream = studentList.stream()
.filter(e -> e.getAge() > 18)
.collect(Collectors.toSet());
//返回其他的类型
}