第十四章 Stream API
JAVA语言引入了一个流式Stream API,这个API对集合数据进行操作,类似于使用SQL执行的数据库查询,同样可以使用Stream API并行执行操作。
Stream和Collection的区别
Collection:静态的内存数据结构,强调的是数据。
Stream API:和集合相关的计算操作,强调的是计算。
Collection面向的是内存,存储在内存中;Stream API面向的是CPU,通过CPU来计算。
Stream API的操作步骤
第一步:创建Stream
通过数据源(如集合、数组等)来获取一个Stream对象。
第二步:中间操作
对数据源的数据进行处理,该操作会返回一个Stream对象,因此可以进行链式操作。
第三步:终止操作
执行终止操作时,才会真正执行中间操作,并返回一个计算完毕后的结果。
Stream API的重要特点
- Stream自己不会存储元素,只能对元素进行计算.
- Stream不会改变数据对象,反而可能会返回一个持有结果的新Stream.
- Stream上的操作属于延迟执行,只有等到用户真正需要结果的时候才会执行.
- Stream一旦执行了终止操作,则就不能再调用其它中间操作或中止操作了.
顺序流和并行流
在前面获得Stream对象的方式,我们都称之为“顺序流",顺序流对Stream元素的处理是单线程的,即一个一个元素进行处理,处理数据的效率较低,如果Stream流中的数据处理没有顺序要求,并且还希望可以并行处理Stream的元素,那么就可以使用"并行流"来实现,从而提高处理数据的效率。
一个普通Stream转换为可以并行处理的stream非常简单,只需要用调用Stream提供的parallel()方法进行转换即可,这样就可以并行的处理Stream的元素,不需要编写任何多线程代码就可以享受到并行处理带来的执行效率的提升。
获取Stream的方式
通过Collection接口的stream()方法来获取Stream对象
public class StreamAPITest01 {public static void main(String[] args) {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);list.add(3);list.add(4);// 注意,这个流属于顺序流,本质是单线程的。数据量如果不是很多,采用这种方式。Stream<Integer> stream = list.stream();// java.util.stream.ReferencePipeline$Head@b4c966a// 通过Stream对象可以对集合中的元素进行计算。System.out.println(stream); // 输出stream对象的内存地址// 这是一个并行流(底层自动启动多线程,你不需要管,程序员不需要干涉)// 在计算的时候自动会启动多线程去运算。// 什么时候用?如果数据量非常庞大。Stream<Integer> parallelStream = list.parallelStream();// java.util.stream.ReferencePipeline$Head@2f4d3709System.out.println(parallelStream);}
}
通过Arrays数组工具类的stream()方法
public class StreamAPITest02 {public static void main(String[] args) {String[] names = {"zhangsan", "lisi", "wangwu"};Stream<String> stream = Arrays.stream(names); // 上面集合对应的流对象System.out.println(stream);int[] nums = {1,2,3,4};IntStream stream1 = Arrays.stream(nums);System.out.println(stream1);long[] lnums = {1L,2L,3L};LongStream stream2 = Arrays.stream(lnums);System.out.println(stream2);}
}
使用Stream接口本身的of(可变长度参数)方法
public class StreamAPITest03 {public static void main(String[] args) {// java.util.stream.ReferencePipeline$Head@b4c966a// stream是一个顺序流,单线程Stream<String> stream = Stream.of("abc", "def", "xyz");System.out.println(stream);System.out.println(stream.isParallel()); // falseStream<String> parallel = stream.parallel(); // 将stream对象变为并行流System.out.println(parallel);System.out.println(stream == parallel); // trueSystem.out.println(parallel.isParallel()); // true// java.util.stream.ReferencePipeline$Head@2f4d3709Stream<Integer> integerStream = Stream.of(1, 2, 3, 4);System.out.println(integerStream);}
}
Stream中间操作
Student类
public class Student implements Comparable<Student>{private String name;private int age;private String gender;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +'}';}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 String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Student(String name, int age, String gender) {this.name = name;this.age = age;this.gender = gender;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Objects.equals(name, student.name) && Objects.equals(gender, student.gender);}@Overridepublic int hashCode() {return Objects.hash(name, age, gender);}@Overridepublic int compareTo(Student o) {return this.getAge() - o.getAge();}
}
StudentService类
public class StudentService {public static List<Student> getStudents(){List<Student> studentList = new ArrayList<>();studentList.add(new Student("zhangsan", 20, "男"));studentList.add(new Student("lisi", 21, "女"));studentList.add(new Student("wangwu", 22, "男"));studentList.add(new Student("zhaoliu", 18, "女"));studentList.add(new Student("qianqi", 19, "女"));/*studentList.add(new Student("qianqi", 19, "男"));studentList.add(new Student("qianqi", 19, "男"));studentList.add(new Student("qianqi", 19, "男"));*/return studentList;}
}
filter(过滤)
public class StreamAPITest04 {public static void main(String[] args) {// 筛选出年龄大于20的学生对象// filter 属于中间操作,过滤// forEach 属于终止操作,遍历// filter和forEach都是Stream接口中的方法。// 由于Stream支持链式调用,所以可以一致"."// 匿名内部类的方式StudentService.getStudents().stream().filter(new Predicate<Student>() {@Overridepublic boolean test(Student student) {return student.getAge() > 20;}}).forEach(new Consumer<Student>() { // 必须要有终止操作@Overridepublic void accept(Student student) {System.out.println(student);}});// Lambda表达式方式StudentService.getStudents().stream().filter(student -> student.getAge() > 20).forEach(System.out::println);// 筛选出字符串长度大于3的元素Stream<String> stream = Stream.of("zhangsan", "lisi", "wangwu", "abc");stream.filter(s -> s.length() > 3).forEach(System.out::println);// 筛选出学生名字长度大于4的学生StudentService.getStudents().stream().filter(student -> student.getName().length() > 4).forEach(System.out::println);}
}
map(映射)
public class StreamAPITest05 {public static void main(String[] args) {// 把字符串中的字母全部转化为大写// 匿名内部类的方式Stream.of("abc", "def", "xyz").map(new Function<String, String>() {@Overridepublic String apply(String s) {return s.toUpperCase();}}).forEach(System.out::println);// Lambda表达式方式Stream.of("abc", "def", "xyz").map(String::toUpperCase).forEach(System.out::println);// 获得集合中所有学生的名字StudentService.getStudents().stream().map(Student::getName).forEach(System.out::println);System.out.println("==============");// 需求:获得集合中性别为男的学生名字// 思路:先筛选,后映射// 先filter,然后mapStudentService.getStudents().stream().filter(student -> student.getGender().equals("男")).map(Student::getName).forEach(System.out::println);System.out.println("==================");// 将多个集合中的数据合并到一个流Stream当中// flatMap的方法作用是什么?将多个集合中的所有元素全部放到一个Stream中。List<Integer> list1 = new ArrayList<>();list1.add(1);list1.add(2);list1.add(3);List<Integer> list2 = new ArrayList<>();list1.add(4);list1.add(5);list1.add(6);Stream<List<Integer>> twoListStream = Stream.of(list1, list2);// 匿名内部类方式/*twoListStream.flatMap(new Function<List<Integer>, Stream<?>>() {@Overridepublic Stream<?> apply(List<Integer> integers) {return integers.stream();}}).forEach(System.out::println);*/// Lambda表达式方式twoListStream.flatMap(List<Integer>::stream).forEach(System.out::println);}
}
distinct(去重)
public class StreamAPITest06 {public static void main(String[] args) {// 除去重复的元素Stream.of(1,1,1,1,1,1,1,2).distinct().forEach(System.out::println);// 除去重复的学生(除重后输出学生对象)// 去除重复记录是基于 hashCode + equals方法的。记得重写。StudentService.getStudents().stream().distinct().forEach(System.out::println);// 需求:除去年龄相同的学生(除重后输出学生年龄)// 思路:先映射,后除重// 先把学生映射成对应的年龄,然后再去重StudentService.getStudents().stream().map(Student::getAge).distinct().forEach(System.out::println);}
}
sorted(排序)
public class StreamAPITest07 {public static void main(String[] args) {// 需求:对元素执行“升序”排序Stream.of(1,2,3,4,100,0,-1).sorted().forEach(System.out::println);Stream.of("ccc", "bbb", "abc", "aaa").sorted().forEach(System.out::println); // aaa, abc, bbb, ccc// 需求:按照学生的年龄执行“升序”排序(排序后输出学生对象)// 注意:这里的排序是对学生对象进行排序,排序规则需要指定,Student实现java.lang.Comparable接口。StudentService.getStudents().stream().sorted().forEach(System.out::println);// 需求:按照学生的年龄执行“升序”排序(排序后输出学生年龄)// 先映射,再排序StudentService.getStudents().stream().map(Student::getAge).sorted().forEach(System.out::println);System.out.println("==================");// 需求:对元素执行“升序”排序Stream.of(10, 20, 30, 18, 15).sorted(Integer::compareTo).forEach(System.out::println);// 需求:按照学生的年龄执行“降序”排序(排序后输出学生对象)StudentService.getStudents().stream().sorted((o1,o2) -> o2.getAge() - o1.getAge()).forEach(System.out::println);// 需求:按照学生的年龄执行“升序”排序(排序后输出学生年龄)// 先映射,再排序StudentService.getStudents().stream().map(Student::getAge).sorted().forEach(System.out::println);StudentService.getStudents().stream().map(Student::getAge).sorted(Integer::compareTo).forEach(System.out::println);}
}
concat(合并)
public class StreamAPITest08 {public static void main(String[] args) {Stream<Integer> stream1 = Stream.of(1, 2, 3);Stream<Integer> stream2 = Stream.of(4, 5, 6);Stream.concat(stream1, stream2).forEach(System.out::println);}
}
skip+limit(截取集合的一部分)
public class StreamAPITest09 {public static void main(String[] args) {Stream.of(1,2,3,4,5,6,7,8,9,10).skip(3).limit(3).forEach(System.out::println); // 4 5 6}
}
match
public class StreamAPITest10 {public static void main(String[] args) {// 匹配集合中元素是否都是3// allMatch 匹配所有System.out.println(Stream.of(1, 2, 3, 4).allMatch(value -> value.equals(3))); // falseSystem.out.println(Stream.of(3, 3, 3, 3).allMatch(value -> value.equals(3))); // true// 匿名内部类System.out.println(Stream.of(3, 3, 3, 3).allMatch(new Predicate<Integer>() {@Overridepublic boolean test(Integer value) {return value.equals(3);}}));// 匹配集合中元素是否包含3// anyMatch 匹配其中一个System.out.println(Stream.of(1, 2, 3, 4).anyMatch(value -> value.equals(3))); // true// 匹配集合中元素有没有3// noneMatch 匹配不上System.out.println(Stream.of(1, 2, 3, 4).noneMatch(value -> value.equals(3))); // falseSystem.out.println(Stream.of(1, 2, 3, 4).noneMatch(value -> value.equals(100))); // true// 获取流中第一个元素Optional<Integer> firstOptional = Stream.of(1, 2, 3, 4).findFirst();System.out.println(firstOptional.get());System.out.println(Stream.of(1, 2, 3, 4).findFirst().get());// 需求:匹配学生名字是否都为“zhangsan”System.out.println(StudentService.getStudents().stream().allMatch(student -> student.getName().equals("zhangsan"))); // false// 需求:匹配学生名字是否至少有一个为“zhangsan”System.out.println(StudentService.getStudents().stream().anyMatch(student -> student.getName().equals("zhangsan"))); // true// 需求:匹配学生名字中是否全部都没有“lucy”System.out.println(StudentService.getStudents().stream().noneMatch(student -> student.getName().equals("lucy"))); // true// 需求:获得第一个学生System.out.println(StudentService.getStudents().stream().findFirst().get());Optional<Student> first = StudentService.getStudents().stream().findFirst();/*if (first.isPresent()) {System.out.println(first.get());}*/first.ifPresent(System.out::println);// 需求:获得第四个学生// 思路:跳过前面3个学生,然后再获得第一个元素System.out.println(StudentService.getStudents().stream().skip(3).findFirst().get());}
}
Stream终止操作(reduce)
作用:将流中的所有数据,按照指定的规则,最终计算出一个结果。
其中预定义的函数式接口是 BinaryOperator
public class StreamAPITest11 {public static void main(String[] args) {// 将集合中的所有数字求和 累加器,初始值为0,也就是当前代码中的 xSystem.out.println(Stream.of(1, 2, 3, 4).reduce((x, y) -> x + y).get()); // 10// 使用数学工具类(方法引用)System.out.println(Math.addExact(10, 20)); // 30System.out.println(Stream.of(1, 2, 3, 4).reduce((x, y) -> Math.addExact(x, y)).get()); // 10System.out.println(Stream.of(1, 2, 3, 4).reduce(Math::addExact).get());// 需求:获得集合中所有元素“相乘”的结果System.out.println(Stream.of(1, 2, 3, 4).reduce(Math::multiplyExact).get());// 需求:获得最大长度的元素System.out.println(Stream.of("abc", "def", "hello", "helloworld").reduce((s1, s2) -> s1.length() > s2.length() ? s1 : s2).get());// 需求:获得所有学生的总年龄System.out.println(StudentService.getStudents().stream().map(Student::getAge).reduce(Math::addExact).get());// 需求:获得10和集合中所有元素“相加”的结果System.out.println(Stream.of(1, 2, 3, 4).reduce(10, Math::addExact));}
}
count,max,min方法
public class StreamAPITest12 {public static void main(String[] args) {// 需求:获得元素的个数System.out.println(StudentService.getStudents().stream().count());System.out.println(StudentService.getStudents().size());// 需求:获得年龄“最大”的学生System.out.println(StudentService.getStudents().stream().max((s1, s2) -> s1.getAge() - s2.getAge()).get());// 需求:获得学生的“最大”年龄System.out.println(StudentService.getStudents().stream().map(Student::getAge).max(Integer::compareTo).get());// 需求:获得年龄“最小”的学生System.out.println(StudentService.getStudents().stream().min((s1, s2) -> s1.getAge() - s2.getAge()).get());// 需求:获得学生的“最小”年龄System.out.println(StudentService.getStudents().stream().map(Student::getAge).min(Integer::compareTo).get());}
}
collect 收集
归集:toList/toSet/toMap
public class StreamAPITest13 {public static void main(String[] args) {// 将流Stream中的数据全部收集到一个集合中。// 收集为List集合。(具体是哪种List集合,在这里是不知道的。)List<String> list = Stream.of("zhangsan", "lisi", "wangwu").collect(Collectors.toList()); // collect传入的参数是收集器System.out.println(list);// 收集为Set集合。(具体是哪种Set集合,在这里是不知道的。)Set<String> set = Stream.of("zhangsan", "lisi", "wangwu", "wangwu").collect(Collectors.toSet());System.out.println(set);// 收集为Map集合。// 匿名内部类的方式/*Map<String, String> map = Stream.of("1:zhangsan", "2:lisi", "3:wangwu").collect(Collectors.toMap(new Function<String, String>() {@Overridepublic String apply(String s) {return s.substring(0, s.indexOf(":"));}}, new Function<String, String>() {@Overridepublic String apply(String s) {return s.substring(s.indexOf(":") + 1);}}));*/// Lambda表达式方式Map<String, String> map = Stream.of("1:zhangsan", "2:lisi", "3:wangwu").collect(Collectors.toMap(s -> s.substring(0, s.indexOf(":")), s -> s.substring(s.indexOf(":") + 1)));// 遍历Map集合map.forEach((k, v) -> System.out.println(k + "===>" + v));}
}
toCollection
以指定的集合类型来进行收集
public class StreamAPITest14 {public static void main(String[] args) {// 注意:ArrayList::new 是构造方法引用// 以ArrayList集合进行收集ArrayList<String> arrayList = Stream.of("zhangsan", "lisi", "wangwu", "wangwu").collect(Collectors.toCollection(ArrayList::new));System.out.println(arrayList);// 以LinkedList集合进行收集LinkedList<String> linkedList = Stream.of("zhangsan", "lisi", "wangwu", "wangwu").collect(Collectors.toCollection(LinkedList::new));System.out.println(linkedList);// 以HashSet集合收集 无序,不可重复HashSet<Integer> hashSet = Stream.of(1, 1, 1, 1, 100, 300, 200, 50, 50).collect(Collectors.toCollection(HashSet::new));System.out.println(hashSet);// 以TreeSet集合收集TreeSet<Integer> treeSet = Stream.of(1, 1, 1, 1, 100, 300, 200, 50, 50).collect(Collectors.toCollection(TreeSet::new));System.out.println(treeSet);}
}
public class StreamAPITest15 {public static void main(String[] args) {ArrayList<Student> students = StudentService.getStudents().stream().filter(student -> student.getGender().equals("女")).filter(student -> student.getAge() > 18).sorted((s1, s2) -> s1.getAge() - s2.getAge()).collect(Collectors.toCollection(ArrayList::new));System.out.println(students);}
}
归集:转换为数组形式
public class StreamAPITest16 {public static void main(String[] args) {// 没有指定数组的类型时,默认转换成Object[]数组。Object[] array = Stream.of(1, 2, 3, 4).toArray();System.out.println(Arrays.toString(array));// 转换为指定类型的数组Integer[] array1 = Stream.of(1, 2, 3, 4).toArray(Integer[]::new);System.out.println(Arrays.toString(array1));String[] array2 = Stream.of("a", "b", "c").toArray(String[]::new);System.out.println(Arrays.toString(array2)); // 字符串数组}
}
public class StreamAPITest17 {public static void main(String[] args) {// 需求:获得元素的个数// 可以使用reducelong count = StudentService.getStudents().stream().count();System.out.println("学生总数:" + count);// 也可以使用collectLong count2 = StudentService.getStudents().stream().collect(Collectors.counting());System.out.println("学生总数:" + count2);// 需求:获得学生的平均年龄Double avgAge = StudentService.getStudents().stream().collect(Collectors.averagingDouble(Student::getAge));System.out.println("学生平均年龄:" + avgAge);// 需求:获得最大年龄的学生Student student = StudentService.getStudents().stream().collect(Collectors.maxBy((s1, s2) -> s1.getAge() - s2.getAge())).get();System.out.println("最大年龄的学生:" + student);// StudentService.getStudents().stream().max((s1, s2) -> s1.getAge() - s2.getAge()).get();
// System.out.println("最大年龄的学生02:" + student);// 需求:获得所有学生年龄之和Integer ageSum = StudentService.getStudents().stream().collect(Collectors.summingInt(Student::getAge));System.out.println("所有学生年龄的和:" + ageSum);// 需求:获得年龄的所有的信息DoubleSummaryStatistics collect = StudentService.getStudents().stream().collect(Collectors.summarizingDouble(Student::getAge));System.out.println(collect);System.out.println(collect.getAverage());System.out.println(collect.getMax());System.out.println(collect.getMin());System.out.println(collect.getSum());}
}
分组:groupingBy
public class StreamAPITest18 {public static void main(String[] args) {// 按照性别分组Map<String, List<Student>> map = StudentService.getStudents().stream().collect(Collectors.groupingBy(Student::getGender));map.forEach((k , v) -> System.out.println(k + "===>" + v));}
}
接合:joining
public class StreamAPITest19 {public static void main(String[] args) {// 需求:将所有学生的姓名连接成一个字符串,每个名字之间以“,”连接String s = StudentService.getStudents().stream().map(Student::getName).collect(Collectors.joining(","));System.out.println(s);// 通过 map(Student::getAge) 提取年龄属性 通过 map(String::valueOf) 将整型年龄转为字符串String s1 = StudentService.getStudents().stream().map(Student::getAge).map(String::valueOf).collect(Collectors.joining(","));System.out.println(s1);// 通过 map() 将每个学生对象转换为字符串 再使用 Collectors.toList() 收集为字符串列表List<String> l1 = StudentService.getStudents().stream().map(student->student.getName() + "," + student.getAge()).collect(Collectors.toList());System.out.println(l1);}
}