1. Stream概述
Stream流是从JDK8以后才有的新特性,是专业用于对集合或者数组进行便捷操作的。
什么是Stream?
Stream将要处理的元素集合看作一种流,在流的过程中,借助Stream API对流中的元素进行操作,比如:过滤、排序、去重等。
2. Stream流的创建
Stream可以由数组或集合创建
1.通过 java.util.Collection.stream() 方法用集合创建流
如何获取 List的Stream流
如何获取 Set的Stream流
单列集合对象.stream()
如何获取 Map的Stream流
变成单列之后 在 .stream()
2.使用java.util.Arrays.stream(T[] array)方法用数组创建流
如果获取 数组的Stream流
Arrays.stream(数组)
3.使用Stream的静态方法:of()、iterate()、generate()
Stream.of(数组)
代码演示如下:
public static void main(String[] args) {
//1 List的Stream流
List<String> names = new ArrayList<>();
Collections.addAll(names,"张三","李四","王五","赵六");
// 数据转换到流上
Stream<String> stream1 = names.stream();
//2 Set的Stream流
Set<String> set = new HashSet<>();
Collections.addAll(set,"李白","杜甫","白居易","李清照");
// 数据转换到流上
Stream<String> stream2 = names.stream();
//3:如何获取 Map的Stream流
Map<String,Integer> map = new HashMap<>();
map.put("姚明",40);
map.put("刘翔",42);
map.put("郭敬明",44);
// 键集 转换成Stream流
Stream<String> stream31 = map.keySet().stream();
// 值集 转换成Stream流
Stream<Integer> stream32 = map.values().stream();
//处理键值对呢? 键值对对象 叫entry
Stream<Map.Entry<String, Integer>> stream3 = map.entrySet().stream();
//4:数组 的Stream流
String[] array = {"孔子","老子","曾子","孟子"};
//两种方式
Stream<String> stream4 = Arrays.stream(array);
Stream<String> stream5 = Stream.of(array);
}
3. Stream流中间方法
3.1 filter(过滤)
Stream filter(Predicate<? super T> predicate): 是按照一定的规则校验流中的元素,将符合条件的元素提取到新的流中的操作
代码演示:
// 找出成绩大于等于60分的数据,并升序后,再输出。
List<Double> scores = new ArrayList<>();
Collections.addAll(scores,88.5,69.4,12.5,89.5,56.3);
scores.stream().filter(sc -> sc >= 60).forEach(s-> System.out.println(s));
运行结果:
3.2 distinct、skip、limit、concat(去重、合并)
- Stream distinct(): 去除流中重复的元素。
- Stream skip(long n):跳过前几个元素
- Stream limt(long max maxSize): 获取前几个元素
- static Stream concat(Stream a,Stream b):合并a和b两个流为一个流
代码演示 :
//创建一个集合 存储多个数据到集合 再把数据放到Stream流中
List<String> list = new ArrayList<>();
Collections.addAll(list,"a","b","c","d","d","c");
// limit(long maxSize):获取前n个元素
System.out.println("------------limit--------------");
list.stream().limit(2).forEach(s-> System.out.println(s));
//skip(long n):跳过前n个元素
System.out.println("-------------skip--------------");
list.stream().skip(3).forEach(s -> System.out.println(s));
//distinct():去除重复
System.out.println("--------------distinct---------");
list.stream().distinct().forEach(s -> System.out.println(s));
//concat:合并a和b两个流为一个流
System.out.println("------------concat--------------");
Stream<String> stream1 = Stream.of("a", "b", "c");
Stream<String> stream2 = Stream.of("1", "2", "3");
Stream<String> concat = Stream.concat(stream1, stream2);
运行结果:
3.3 sorted(排序)
- Stream sorted() :对元素进行升序排序
- Stream sorted(Comparator<? super T> comparator) :按照指定规则排序
代码演示:
//sorted():对元素进行升序排序
System.out.println("---------------sorted----------");
list.stream().sorted().forEach(s -> System.out.println(s));
System.out.println("----- sorted(排序规则)-----------");
//既要去重,又要降重 distinct sorted(排序规则)
list.stream().distinct().sorted(((o1, o2) -> o2.compareTo(o1))).forEach(s -> System.out.println(s));
运行结果:
3.4 map、flatMap(映射)
映射,可以将一个流的元素按照一定的映射规则映射到另一个流中。分为map和flatMap:
map:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。flatMap:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
图示:
代码演示:
//map;映射的意思,一一对应
System.out.println("------------map-----------------");
scores.stream().map(s->{return new Student("张三",18,s);}).forEach(s-> System.out.println(s));
运行结果:
代码演示:
List<String> list = Arrays.asList("m,k,l,a", "1,3,5,7");
List<String> listNew = list.stream().flatMap(s -> {
// 将每个元素转换成一个stream
String[] split = s.split(",");
Stream<String> s2 = Arrays.stream(split);
return s2;
}).collect(Collectors.toList());
System.out.println("处理前的集合:" + list);
System.out.println("处理后的集合:" + listNew);
运行结果:
4.Stream流终结方法
4.1 max、min、count(聚合)
- count:统计此流运算的元素个数
- max:获取此流运算后的最大值元素
- min:获取此流运算后的最小值元素
代码演示:
List<Student> students = new ArrayList<>();
Student s1 = new Student("蜘蛛精", 26, 72.5);
Student s2 = new Student("蜘蛛精", 26, 72.5);
Student s3 = new Student("紫霞", 23, 67.6);
Student s4 = new Student("白晶晶", 25, 69.0);
Student s5 = new Student("牛魔王", 35, 83.3);
Student s6 = new Student("牛夫人", 34, 68.5);
Collections.addAll(students, s1, s2, s3, s4, s5, s6);
// 需求1:请计算出分数超过68的学生有几人。
long count = students.stream().filter(s -> s.getScore() > 68).count();
System.out.println("分数超过68的有 "+count+" 人");
// 需求2:请找出分数最高的学生对象,并输出。
Student student = students.stream().max(((o1, o2) -> Double.compare(o1.getScore(), o2.getScore()))).get();
System.out.println("分数最高的学生信息:"+student);
// 需求3:请找出分数最低的学生对象,并输出。
Student student1 = students.stream().min(((o1, o2) -> Double.compare(o1.getScore(), o2.getScore()))).get();
System.out.println("分数最高的学生信息:"+student1);
运行结果:
4.2 foreach、find、match(遍历匹配)
- foreach:对此流运算后的元素执行遍历
- findFirst:匹配第一个
- findAny:匹配任意(适用于并行流)
- anyMatch:是否包含符合特定条件的元素
代码演示:
List<Integer> list = Arrays.asList(7, 6, 9, 3, 8, 2, 1);
// 遍历输出符合条件的元素
list.stream().filter(x -> x > 6).forEach(System.out::println);
// 匹配第一个
Optional<Integer> findFirst = list.stream().filter(x -> x > 6).findFirst();
// 匹配任意(适用于并行流)
Optional<Integer> findAny = list.parallelStream().filter(x -> x > 6).findAny();
// 是否包含符合特定条件的元素
boolean anyMatch = list.stream().anyMatch(x -> x > 6);
System.out.println("匹配第一个值:" + findFirst.get());
System.out.println("匹配任意一个值:" + findAny.get());
System.out.println("是否存在大于6的值:" + anyMatch);
运行结果:
4.3 reduce(归约)
归约,也称缩减,顾名思义,是把一个流缩减成一个值,能实现对集合求
和、求乘积和求最值操作。
图示:
代码演示:
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 3, 2, 8, 11, 4);
// 求和方式1
Optional<Integer> sum = list.stream().reduce((x, y) -> x + y);
// 求和方式2
Optional<Integer> sum2 = list.stream().reduce(Integer::sum);
// 求和方式3
Integer sum3 = list.stream().reduce(0, Integer::sum);
// 求乘积
Optional<Integer> product = list.stream().reduce((x, y) -> x * y);
// 求最大值方式1
Optional<Integer> max = list.stream().reduce((x, y) -> x > y ? x : y);
// 求最大值写法2
Integer max2 = list.stream().reduce(1, Integer::max);
System.out.println("list求和:" + sum.get() + "," + sum2.get() + "," + sum3);
System.out.println("list求积:" + product.get());
System.out.println("list求和:" + max.get() + "," + max2);
}
运行结果:
4.4 collect(收集)
collect,收集,可以说是内容最繁多、功能最丰富的部分了。从字面上去理解,就是把一个流收集起来,最终可以是收集成一个值也可以收集成一个新的集合。
4.4.1 toList、toSet、toMap(归集)
代码演示:
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 6, 3, 4, 6, 7, 9, 6, 20);
List<Integer> listNew = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toList());
Set<Integer> set = list.stream().filter(x -> x % 2 == 0).collect(Collectors.toSet());
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
personList.add(new Person("Anni", 8200, 24, "female", "New York"));
Map<?, Person> map = personList.stream().filter(p -> p.getSalary() > 8000)
.collect(Collectors.toMap(Person::getName, p -> p));
System.out.println("toList:" + listNew);
System.out.println("toSet:" + set);
System.out.println("toMap:" + map);
}
运行结果:
4.4.2 count、averaging(统计)
- 计数:count
- 平均值:averagingInt、averagingLong、averagingDouble
- 最值:maxBy、minBy
- 求和:summingInt、summingLong、summingDouble
- 统计以上所有:summarizingInt、summarizingLong、summarizingDouble
代码演示:
public class StreamTest {
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
// 求总数
Long count = personList.stream().collect(Collectors.counting());
// 求平均工资
Double average = personList.stream().collect(Collectors.averagingDouble(Person::getSalary));
// 求最高工资
Optional<Integer> max = personList.stream().map(Person::getSalary).collect(Collectors.maxBy(Integer::compare));
// 求工资之和
Integer sum = personList.stream().collect(Collectors.summingInt(Person::getSalary));
// 一次性统计所有信息
DoubleSummaryStatistics collect = personList.stream().collect(Collectors.summarizingDouble(Person::getSalary));
System.out.println("员工总数:" + count);
System.out.println("员工平均工资:" + average);
System.out.println("员工工资总和:" + sum);
System.out.println("员工工资所有统计:" + collect);
}
}
运行结果:
4.4.3 partitioningBy、groupingBy(分组)
- 分区:将
stream按条件分为两个Map,比如员工按薪资是否高于8000分为两部分。 - 分组:将集合分为多个Map,比如员工按性别分组。有单级分组和多级分组。 图示:
代码演示:
public class StreamTest {
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, "male", "New York"));
personList.add(new Person("Jack", 7000, "male", "Washington"));
personList.add(new Person("Lily", 7800, "female", "Washington"));
personList.add(new Person("Anni", 8200, "female", "New York"));
personList.add(new Person("Owen", 9500, "male", "New York"));
personList.add(new Person("Alisa", 7900, "female", "New York"));
// 将员工按薪资是否高于8000分组
Map<Boolean, List<Person>> part = personList.stream().collect(Collectors.partitioningBy(x -> x.getSalary() > 8000));
// 将员工按性别分组
Map<String, List<Person>> group = personList.stream().collect(Collectors.groupingBy(Person::getSex));
// 将员工先按性别分组,再按地区分组
Map<String, Map<String, List<Person>>> group2 = personList.stream().collect(Collectors.groupingBy(Person::getSex, Collectors.groupingBy(Person::getArea)));
System.out.println("员工按薪资是否大于8000分组情况:" + part);
System.out.println("员工按性别分组情况:" + group);
System.out.println("员工按性别、地区:" + group2);
}
}
运行结果:
4.4.4joining(接合)
joining可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。
代码演示:
public class StreamTest {
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
String names = personList.stream().map(p -> p.getName()).collect(Collectors.joining(","));
System.out.println("所有员工的姓名:" + names);
List<String> list = Arrays.asList("A", "B", "C");
String string = list.stream().collect(Collectors.joining("-"));
System.out.println("拼接后的字符串:" + string);
}
}
运行结果:
4.4.5reducing(规约)
Collectors类提供的reducing方法,相比于stream本身的reduce方法,增加了对自定义归约的支持。
代码演示:
public class StreamTest {
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("Tom", 8900, 23, "male", "New York"));
personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
// 每个员工减去起征点后的薪资之和(这个例子并不严谨,但一时没想到好的例子)
Integer sum = personList.stream().collect(Collectors.reducing(0, Person::getSalary, (i, j) -> (i + j - 5000)));
System.out.println("员工扣税薪资总和:" + sum);
// stream的reduce
Optional<Integer> sum2 = personList.stream().map(Person::getSalary).reduce(Integer::sum);
System.out.println("员工薪资总和:" + sum2.get());
}
}
运行结果: