Java8
中添加了一个新的接口类 Stream
,相当于高级版的Iterator
,通过Lambda
表达式对集合进行各种非常便利、高效的聚合操作(Aggregate Operation)
,或者大批量数据操作(Bulk Data Operation)
。Stream
不仅可以通过串行的方式实现数据操作,还可以通过并行的方式处理大批量数据,提高数据的处理效率。
在《java8 in action》书中,作者说目前我们在几乎所有开发中都会用到集合,但是目前集合在程序开发中的表现还不够完美
,比如你利用集合处理大量数据时,你不得不面对性能问题,不得不考虑进行并行代码的编写,这些工作都是比较繁重的,于是作者便创造了Stream 流。
一、Stream流
-
在Stream流中无法直接修改集合、数组等数据源中的数据,必须重新定义一个来接收结果集。
-
stream不存储数据,而是按照特定的规则对数据进行计算,一般会输出结果;
-
stream不会改变数据源,通常情况下会产生一个新的集合;
-
stream具有延迟执行特性,只有调用终端操作时,中间操作才会执行。
-
对stream操作分为终端操作和中间操作,那么这两者分别代表什么呢?
终端操作:会消费流,这种操作会产生一个结果的,如果一个流被消费过了,那它就不能被重用的。
中间操作:中间操作会产生另一个流。因此中间操作可以用来创建执行一系列动作的管道。一个特别需要注意的点是:中间操作不是立即发生的。相反,当在中间操作创建的新流上执行完终端操作后,中间操作指定的操作才会发生。所以中间操作是延迟发生的,中间操作的延迟行为主要是让流API能够更加高效地执行。 -
stream不可复用,对一个已经进行过终端操作的流再次调用,会抛出异常。
二、函数
1、Collect
1.1、ToList
//tolist
List<Integer> result = list.stream.filter(number -> number % 2 == 0).collect(Collector.toList);
//tomap
Map<String, Integer> map = list.stream().filter(
s -> {
String[] split = s.split(",");
int age = Integer.parseInt(split[1]);
return age >= 24;
}
).collect(Collector.toMap(
s -> s.split(",")[0],
(String s) -> {
return Integer.parseInt(s.split(",")[1]);
}
));
//toset
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());
1.2、Count
Collectors提供了一系列用于数据统计的静态方法:
计数: count
平均值:averagingInt、averagingLong、averagingDouble
最值: maxBy、minBy
求和: summingInt、summingLong、summingDouble
统计以上所有:summarizingInt、summarizingLong、summarizingDouble
案例:统计员工人数、平均工资、工资总额、最高工资。
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);
运行结果:
员工总数:3
员工平均工资:7900.0
员工工资总和:23700
员工工资所有统计:DoubleSummaryStatistics{count=3, sum=23700.000000,min=7000.000000, average=7900.000000, max=8900.000000}
1.3、GroupingBy
partitioningBy/groupingBy
- 分区:将
stream
按条件分为两个Map
,比如员工按薪资是否高于8000分为两部分。 - 分组:将集合分为多个Map,比如员工按性别分组。有单级分组和多级分组。
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);
案例二:
//分组数目
System.out.println("分组数目:");
Integer groupCount = peopleList
.stream()
.collect(Collectors
.collectingAndThen(Collectors.groupingBy(People::getName), Map::size));
System.out.println(groupCount);
System.out.println("-------------------------------------");
//按照名字分组
System.out.println("按照名字分组");
System.out.println(peopleList.stream()
.collect(Collectors.groupingBy(People::getName))
);
System.out.println("-------------------------------------");
//按照名字分组(分组的结果是一个map),并统计每一个分组(map中的每一个value)中的元素数目
System.out.println("统计每一个分组(map中的每一个value)中的元素数目");
System.out.println(peopleList.stream()
.collect(Collectors.groupingBy(People::getName, Collectors.counting())));
System.out.println("-------------------------------------");
//按照名字分组(分组的结果是一个map),并取出每一组的最大值
System.out.println("取出每一组的最大值");
System.out.println(peopleList.stream()
.collect(Collectors
.groupingBy(People::getName, Collectors.maxBy(
new Comparator<People>() {
@Override
public int compare(People o1, People o2) {
return o1.getAge() - o2.getAge();
}
}))));
//7
//{小猪=[People{age=31, gender=0, name='小猪'},People{age=20, gender=0, name='小猪'},People{age=22, gender=0, name='小猪'}]}
//{小猪=2,小龙=3,小白=4,小王=5}
//{小猪=Optional[People{age=20, gender=0, name='小猪'}],小龙=Optional[People{age=30, gender=0, name='小龙'}]}
1.4、Joining
joining可以将stream中的元素用特定的连接符(没有的话,则直接连接)连接成一个字符串。
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);
运行结果:
所有员工的姓名:Tom,Jack,Lily
拼接后的字符串:A-B-C
1.5、Reducing
Collectors类提供的reducing方法,相比于stream本身的reduce方法,增加了对自定义归约的支持。
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());
运行结果:
员工扣税薪资总和:8700
员工薪资总和:23700
2、Filter
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
// 获取空字符串的数量
long count = strings.stream()
.filter(string -> string.isEmpty())
.count();
// filter().limit(2)
案例二: 筛选员工中工资高于8000的人,并形成新的集合。 形成新集合依赖collect
(收集),后文有详细介绍。
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"));
personList.add(new Person("Owen", 9500, 25, "male", "New York"));
personList.add(new Person("Alisa", 7900, 26, "female", "New York"));
List<String> fiterList = personList.stream()
.filter(x -> x.getSalary() > 8000)
.map(Person::getName)
.collect(Collectors.toList());
System.out.print("高于8000的员工姓名:" + fiterList);
3、Reduce
归约,也称缩减,顾名思义,是把一个流缩减成一个值,能实现对集合求和、求乘积和求最值操作。
案例一:求Integer集合的元素之和、乘积和最大值。
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、Count
过滤出来数据数量合
long count = strings.stream().filter(string -> string.isEmpty()).count();
public class StreamTest {
public static void main(String[] args) {
List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
Optional<String> max = list.stream().max(Comparator.comparing(String::length));
System.out.println("最长的字符串:" + max.get());
}
}
5、ForEach
String流的生命周期:同一个流只能遍历一次,遍历完后,这个流就已经被消费掉了。你如果还需要在遍历,可以从原始数据源那里再获得一个新的流来重新遍历一遍。
Random random = new Random(); random.ints().limit(10).forEach(System.out::println);
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);
6、Map
map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素对应的平方数:
map
:接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。flatMap
:接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流。
// 获取对应的平方数
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
List<Integer> squaresList = numbers.stream().map( i -> i*i).distinct().collect(Collectors.toList());
//获取10个姓王的人并输出。
List<Person> personList = fromDB(); // 获得List<Person>
personList.stream()
.filter(item->item.age == 70) // 过滤条件
.limit(10) // limit限制条件
.filter(item->item.name.startWith("王")) // 过滤条件
.map(item->item.name) // 获得姓名
.forEach(System.out::println);
//获取一个String 类型的Stream流
//使用map方法,把字符串类型的整数,转换(映射)为Integer类型的整数
//遍历stream2流
Stream<String> stream = Stream.of("1", "2", "3", "4");
Stream<Integer> stream2 = stream.map(s-> Integer.parseInt(s));
stream2.forEach(i-> System.out.println(i));
7、Concat
//concat
Stream.concat(stream1, stream2).forEach(name ->{
Actor actor = new Actor(name);
System.out.println(name)
});
//limit
Random random = new Random();
random.ints().limit(10).forEach(System.out::println);
String[] arr1 = { "a", "b", "c", "d" };
String[] arr2 = { "d", "e", "f", "g" };
Stream<String> stream1 = Stream.of(arr1);
Stream<String> stream2 = Stream.of(arr2);
// concat:合并两个流 distinct:去重
List<String> newList = Stream.concat(stream1, stream2)
.distinct()
.collect(Collectors.toList());
// limit:限制从流中获得前n个数据
List<Integer> collect = Stream.iterate(1, x -> x + 2)
.limit(10)
.collect(Collectors.toList());
// skip:跳过前n个数据
List<Integer> collect2 = Stream.iterate(1, x -> x + 2)
.skip(1)
.limit(5)
.collect(Collectors.toList());
System.out.println("流合并:" + newList);
System.out.println("limit:" + collect);
System.out.println("skip:" + collect2);
8、Sorted
Random random = new Random();
random.ints().limit(10).sorted().forEach(System.out::println);
9、Collectors
Collectors 类实现了很多归约操作,例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串:
List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
System.out.println("筛选列表: " + filtered);
//筛选出来的string字符进行拼接
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
System.out.println("合并字符串: " + mergedString);
三、统计
List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());
四、流创建
1、随机数流
Random rand = new Random(47);
show(rand.ints().boxed());
show(rand.longs().boxed());
show(rand.doubles().boxed());
// 控制上限和下限:
show(rand.ints(10, 20).boxed());
show(rand.longs(50, 100).boxed());
show(rand.doubles(20, 30).boxed());
// 控制流大小:
show(rand.ints(2).boxed());
show(rand.longs(2).boxed());
show(rand.doubles(2).boxed());
// 控制流的大小和界限
show(rand.ints(3, 3, 9).boxed());
show(rand.longs(3, 12, 22).boxed());
show(rand.doubles(3, 11.5, 12.3).boxed());
具体看on java 8 第14章,流创建、随机数流
2、int类型的范围
public class Ranges {
public static void main(String[] args) {
// 传统方法:
int result = 0;
for (int i = 10; i < 20; i++)
result += i;
System.out.println(result);
// for-in 循环:
result = 0;
for (int i : range(10, 20).toArray())
result += i;
System.out.println(result);
// 使用流:
System.out.println(range(10, 20).sum());
}
}