Java 8 新特性|java.util.stream.Collectors
Java 8 流的新类 java.util.stream.Collectors 实现了 java.util.stream.Collector 接口,同时又提供了大量的方法对流 ( stream ) 的元素执行 map and reduce 操作,或者统计操作。
1、 Collectors.averagingDouble()
Collectors.averagingDouble() 方法将流中的所有元素视为 double 类型并计算他们的平均值。该方法返回的是同一个 Collectors 实例,因此可以进行链式操作。
Collectors.averagingDouble() 接受一个参数,这个参数是一个 lambda 表达式,用于对所有的元素执行一个 map 操作。
Java 所有集合的 stream().collect() 可以接受一个收集器实例作为其参数并返回该收集器的计算结果
参考下面的代码,collect() 方法会把所有的元素收集起来然后传递给 Collectors.averagingDouble(d->d*2) 收集器,对每个元素执行 *2 操作后计算平均值
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class AveragingDoubleTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4);
Double result = list.stream().collect(Collectors.averagingDouble(d->d*2));
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.AveragingDoubleTest
5.0
Process finished with exit code 0
2、 Collectors.averagingInt()
该方法和Collectors.averagingDouble()一样,不同的是它把流中的所有元素看成是 int 类型,并返回一个浮点类型的平均值
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class AveragingIntTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4);
Double result = list.stream().collect(Collectors.averagingInt(v->v*2));
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.AveragingIntTest
5.0
Process finished with exit code 0
3、 Collectors.averagingLong()
该方法也和Collectors.averagingDouble()类似,不同的是它把流中的所有元素看成是 long 类型,并返回一个 double 类型的平均值
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class AveragingLongTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4);
Double result = list.stream().collect(Collectors.averagingLong(v->v*2));
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.AveragingLongTest
5.0
Process finished with exit code 0
4、 Collectors.averagingDouble()
Collectors.collectingAndThen()函数应该最像 map and reduce 了,它可接受两个参数,第一个参数用于 reduce 操作,而第二参数用于 map 操作。
也就是,先把流中的所有元素传递给第二个参数,然后把生成的集合传递给第一个参数来处理。
例如下面的代码,先把 [1,2,3,4] 这个集合传递给 s-> ss lambda 表达式,计算得出结果为 [1,4,9,16] ,然后再把 [1,4,9,16] 传递给 v->v2 表达式,计算得出 [2,8,18,32] ,然后传递给 Collectors.averagingLong() 计算得到结果为 25.0
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CollectingAndThenTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4);
Double result = list.stream().collect(Collectors.collectingAndThen(Collectors.averagingLong(v->v*2),
s-> s*s));
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.CollectingAndThenTest
25.0
Process finished with exit code 0
5、 Collectors.counting()
Collectors.counting() 用于统计流中元素的个数。
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class CountingTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4);
long result= list.stream().collect(Collectors.counting());
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.CountingTest
4
Process finished with exit code 0
6、 Collectors.joining()
Collectors.joining() 方法用某个指定的拼接字符串把所有元素拼接成一个字符串,并添加可选的前缀和后缀
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class JoiningTest {
public static void main(String[] args) {
List<String> list = Arrays.asList("A","B","C","D");
String result= list.stream().collect(Collectors.joining(",","(",")"));
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.JoiningTest
(A,B,C,D)
Process finished with exit code 0
7、 Collectors.maxBy() 和 Collectors.minBy()
Collectors.maxBy()和Collectors.minBy() 两个方法分别用于计算流中所有元素的最大值和最小值。
两个方法都可以接受一个比较器作为参数,用于如何计算最大值或最小值
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class MaxByMinByTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(30,10,20,35);
//Get Max
list.stream().collect(Collectors.maxBy(new MaxByMinByTest().new IntegerComp()))
.ifPresent(i->System.out.println(i));
//Get Min
list.stream().collect(Collectors.minBy(new MaxByMinByTest().new IntegerComp()))
.ifPresent(i->System.out.println(i));
}
class IntegerComp implements Comparator<Integer> {
@Override
public int compare(Integer i1, Integer i2) {
if(i1 >=i2 ){
return 1;
}else{
return -1;
}
}
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.MaxByMinByTest
35
10
Process finished with exit code 0
8、 Collectors.summingInt()
Collectors.summingInt() 方法将流中的所有元素视为 int 类型,并计算所有元素的总和 ( sum )
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SummingIntTest {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(30,10,20,35);
int result = list.stream().collect(Collectors.summingInt(i->i));
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.SummingIntTest
95
Process finished with exit code 0
9、 Collectors.summingLong()
Collectors.summingLong() 将流中的所有元素视为 long 类型,并计算所有元素的总和
package com.sjh.test.java8.collectors;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SummingLongTest {
public static void main(String[] args) {
List<Long> list = new ArrayList<>();
list.add((long)340);
list.add((long)240);
list.add((long)360);
long result = list.stream().collect(Collectors.summingLong(l->l));
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.SummingLongTest
940
Process finished with exit code 0
10、 Collectors.summingDouble()
Collectors.summingDouble() 将流中的所有元素视为 double 类型,并计算所有元素的总和
package com.sjh.test.java8.collectors;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SummingDoubleTest {
public static void main(String[] args) {
List<Double> list = Arrays.asList(340.5,234.56,672.76);
Double result = list.stream().collect(Collectors.summingDouble(d->d));
System.out.println(result);
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.SummingDoubleTest
1247.82
Process finished with exit code 0
summingInt()、summingLong()、summingDouble()这三个函数的结果的类型,就是它们如何看待元素的类型。
11、 Collectors.toList()
Collectors.toList() 将流中的所有元素导出到一个列表 ( List ) 中
package com.sjh.test.java8.collectors;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ToListTest {
public static void main(String[] args) {
List<String> list = Stream.of("AA","BB","CC").collect(Collectors.toList());
list.forEach(s->System.out.println(s));
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.ToListTest
AA
BB
CC
Process finished with exit code 0
12、 Collectors.toSet()
Collectors.toSet() 把流中的所有元素导出到一个集合 ( Set ) 中,并排除重复的元素 ( Set 的特性 )
package com.sjh.test.java8.collectors;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ToSetTest {
public static void main(String[] args) {
Set<String> set = Stream.of("AA","AA","BB").collect(Collectors.toSet());
set.forEach(s->System.out.println(s));
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.ToSetTest
AA
BB
Process finished with exit code 0
13、 Collectors.toMap()
Collectors.toMap() 将流中的所有元素导出到一个哈希表 ( Map ) 中。该方法接受两个参数,第一个参数用于生成键 ( key ) ,第二个参数用于生成值 ( value )。两个参数都是 Lambda 表达式。
package com.sjh.test.java8.collectors;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ToMapTest {
public static void main(String[] args) {
Map<String,String> map = Stream.of("AA","BB","CC").collect(Collectors.toMap(k->k, v->v+v));
map.forEach((k,v)->System.out.println("key:"+k +" value:"+v));
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.ToMapTest
key:CC value:CCCC
key:BB value:BBBB
key:AA value:AAAA
Process finished with exit code 0
14、 Collectors.mapping()
Collectors.mapping() 一般用于多重 map and reduce 中。Java 文档中描述的原型如下:
mapping(Function<? super T,? extends U> mapper, Collector<? super U,A,R> downstream)
第一个参数用于 map ,第二个参数用于 reduce
package com.sjh.test.java8.collectors;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class MappingTest {
public static void main(String[] args) {
List<Person> list = Person.getList();
Map<Integer, String> nameByAge
= list.stream().collect(Collectors.groupingBy(Person::getAge,
Collectors.mapping(Person::getName, Collectors.joining(","))));
nameByAge.forEach((k,v)->System.out.println("Age:"+k +" Persons: "+v));
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public static List<Person> getList() {
List<Person> list = new ArrayList<>();
list.add(new Person("JayLa", 30));
list.add(new Person("LayMan", 20));
list.add(new Person("Tom", 20));
list.add(new Person("XinYi", 30));
return list;
}
}
运行结果:
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/...com.sjh.test.java8.collectors.MappingTest
Age:20 Persons: LayMan,Tom
Age:30 Persons: JayLa,XinYi
Process finished with exit code 0
我是二哥(Jayla),每天总结一点点,每天进步一点点。
我是二哥(Jayla),欢迎大家关注我的公众号:
每天总结一点点,每天进步一点点。
专注Java相关知识,每天准时分享干货。 SpringBoot/Oracle/Git/Linux/Mysql 期待您的加入~
推荐阅读
-
Java8新特性(三)集合之 Stream 流式操作
-
JAVA 8 新特性Stream初体验
-
Flash 8新特性精彩GIF动画实例展示
-
聊聊 Java8 以后各个版本的新特性
-
Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全
-
Java8新特性Stream的完全使用指南
-
Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析
-
JavaScript ES2019中的8个新特性详解
-
Java8新特性Lambda表达式的一些复杂用法总结
-
函数式接口 + Lamda表达式推导过程(Java8 新特性)