记录一下 java8 Collectors 收集器使用详解。
1.Collectors.toList()
List result =givenList.stream()
.collect(toList());
2.Collectors.toSet()
Set result =givenList.stream()
.collect(toSet());
3.Collectors.toMap()
toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction, Supplier mapSupplier)
The arguments are as follows.
Function keyMapper: It generates key for Map.
Function valueMapper: It generates value for Map.
BinaryOperator mergeFunction: This is optional. The usability of merge function is to handle the situation of duplicate Map keys. Using BinaryOperator we can merge the values of duplicate keys. If we do not pass this argument, then by default it throws IllegalStateException in case of duplicate keys.
Supplier mapSupplier: This is optional. It returns a Map in which data is filled as key/value. If we do not pass map supplier then the default supplier will return HashMap. If we want to get any other instance such as LinkedHashMap, we need to pass supplier as LinkedHashMap::new.
3.1 toMap(Function keyMapper, Function valueMapper
ListToMap1.java
packagecom.concretepage;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.function.Function;importjava.util.stream.Collectors;public classListToMap1 {public static voidmain(String[] args) {
List list = new ArrayList<>();
list.add("Mohan");
list.add("Sohan");
list.add("Mahesh");
Map map = list.stream().collect(Collectors.toMap(Function.identity(), s->s));
map.forEach((x, y)-> System.out.println("Key: " + x +", value: "+y));
}
}
output
Key: Mohan, value: Mohan
Key: Mahesh, value: Mahesh
Key: Sohan, value: Sohan
ListToMap2.java
packagecom.concretepage;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.stream.Collectors;public classListToMap2 {public static voidmain(String[] args) {
List list = new ArrayList<>();
list.add(new Person(100, "Mohan"));
list.add(new Person(200, "Sohan"));
list.add(new Person(300, "Mahesh"));
Map map =list.stream()
.collect(Collectors.toMap(Person::getId, Person::getName));
map.forEach((x, y)-> System.out.println("Key: " + x +", value: "+y));
}
}
output
Key: 100, value: Mohan
Key:200, value: Sohan
Key:300, value: Mahesh
3.2 toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction)
ListToMapWithBinaryOperator.java
packagecom.concretepage;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importjava.util.stream.Collectors;public classListToMapWithBinaryOperator {public static voidmain(String[] args) {
List list = new ArrayList<>();
list.add(new Person(100, "Mohan"));
list.add(new Person(100, "Sohan"));
list.add(new Person(300, "Mahesh"));
Map map =list.stream()
.collect(Collectors.toMap(Person::getId, Person::getName, (x, y)-> x+", "+y));
map.forEach((x, y)-> System.out.println("Key: " + x +", value: "+y));
}
}
output
Key: 100, value: Mohan, Sohan
Key:300, value: Mahesh
3.3 toMap(Function keyMapper, Function valueMapper, BinaryOperator mergeFunction, Supplier mapSupplier)
ListToMapWithSupplier.java
packagecom.concretepage;importjava.util.ArrayList;importjava.util.LinkedHashMap;importjava.util.List;importjava.util.stream.Collectors;public classListToMapWithSupplier {public static voidmain(String[] args) {
List list = new ArrayList<>();
list.add(new Person(100, "Mohan"));
list.add(new Person(100, "Sohan"));
list.add(new Person(300, "Mahesh"));
LinkedHashMap map =list.stream()
.collect(Collectors.toMap(Person::getId, Person::getName,
(x, y)-> x+", "+ y, LinkedHashMap::new));
map.forEach((x, y)-> System.out.println("Key: " + x +", value: "+y));
}
}
output
Key: 100, value: Mohan, Sohan
Key:300, value: Mahesh
4. Collectors.toCollection()
可以转成具体的实现
List result =givenList.stream()
.collect(toCollection(LinkedList::new))
5. Collectors.partitioningBy()
判断true、false 返回两个集合
Map> youngerThan30 =team.
stream().
collect(Collectors.partitioningBy(d-> d.getAge() < 30));
System.out.println("Developers younger than thirty are using: " + youngerThan30.get(true).stream().
map(d->d.getFavoriteLanguage()).
collect(Collectors.toSet()));//Output: Developers younger than thirty are using: [java]
System.out.println("Developers older than thirty are using: " + youngerThan30.get(false).stream().
map(d->d.getFavoriteLanguage()).
collect(Collectors.toSet()));//Output: Developers older than thirty are using: [logo, javascript]
6.Collectors.groupingBy()
Collector> groupingBy(Function super T,? extends K> classifier, Collector super T,A,D> downstream)
Map> peropleByAge = people.filter(p -> p.age > 12).collect(Collectors.groupingBy(p -> p.age, Collectors.toList()));
Map> peropleByAge = people.collect(Collectors.groupingBy(p -> p.age, Collectors.mapping((Person p) -> p.name, Collectors.toList())));
Map sumAgeByName = people.collect(Collectors.groupingBy(p -> p.name, Collectors.reducing(0, (Person p) ->p.age, Integer::sum)));/*或者使用summingInt方法*/sumAgeByName= people.collect(Collectors.groupingBy(p -> p.name, Collectors.summingInt((Person p) -> p.age)));
Map map = list.stream().collect(groupingBy(Person::getName,counting()));
7.Collectors.joininng()
String result =givenList.stream()
.collect(joining(" "));
String result =givenList.stream()
.collect(joining(" ", "PRE-", "-POST"));
8.Collectors.counting()
Long result =givenList.stream()
.collect(counting());
9.Collectors.reduce()
9.1 reduce(BinaryOperator accumulator)
packagecom.concretepage;importjava.util.Arrays;public classReduceDemo1 {public static voidmain(String[] args) {int[] array = {23,43,56,97,32};
Arrays.stream(array).reduce((x,y)-> x+y).ifPresent(s ->System.out.println(s));
Arrays.stream(array).reduce(Integer::sum).ifPresent(s->System.out.println(s));
Arrays.stream(array).reduce(StatisticsUtility::addIntData).ifPresent(s->System.out.println(s));
}
}
packagecom.concretepage;public classStatisticsUtility {public static int addIntData(int num1, intnum2) {return num1 +num2;
}
}
9.2 reduce(T identity, BinaryOperator accumulator)
ackage com.concretepage;importjava.util.Arrays;public classReduceDemo2 {public static voidmain(String[] args) {int[] array = {23,43,56,97,32};//Set start value. Result will be start value + sum of array.
int startValue = 100;int sum = Arrays.stream(array).reduce(startValue, (x,y) -> x+y);
System.out.println(sum);
sum=Arrays.stream(array).reduce(startValue, Integer::sum);
System.out.println(sum);
sum=Arrays.stream(array).reduce(startValue, StatisticsUtility::addIntData);
System.out.println(sum);
}
}
9.3 reduce(U identity, BiFunction accumulator, BinaryOperator combiner)
packagecom.concretepage;importjava.util.Arrays;importjava.util.List;public classReduceDemo3 {public static voidmain(String[] args) {
List list2 = Arrays.asList(2, 3, 4);//Here result will be 2*2 + 2*3 + 2*4 that is 18.
int res = list2.parallelStream().reduce(2, (s1, s2) -> s1 * s2, (p, q) -> p +q);
System.out.println(res);
List list1 = Arrays.asList("Mohan", "Sohan", "Ramesh");
String result= list1.parallelStream().reduce("-", (s1, s2) -> s1 + s2, (p, q) -> p +q);
System.out.println(result);
}
}
output
18
-Mohan-Sohan-Ramesh
10.Collectors.collectingAndThen()
将收集到的结果转换成另一种形式
public classCollectingAndThenExample {public static voidmain (String[] args) {
Stream s = Stream.of("apple", "banana", "orange");
List synchronizedList =s.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::synchronizedList));
System.out.println(synchronizedList);
}
}
public classCollectingAndThenExample {public static voidmain(String[] args) {
List 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);
}
}
11.Collectors.summarizingDouble/Long/Int()
统计数据时常用
getAverage(): It returns the average of all accepted value.
getCount(): It calculates the count of all element.
getMax(): It returns the maximum value.
getMin(): It returns the minimum value.
getSum(): It returns the sum of all elements.
accept(): It adds the element into the summary information.
combine(): It combines two summary statistics.
11.1 primitive data type
public classSummaryStatisticsWithPrimitiveDataType {public static voidmain(String[] args) {
System.out.println("---DoubleSummaryStatistics---");
DoubleSummaryStatistics dstats= DoubleStream.of(5.33d,2.34d,5.32d,2.31d,3.51d).
collect(DoubleSummaryStatistics::new, DoubleSummaryStatistics::accept,
DoubleSummaryStatistics::combine);
System.out.println("Max:"+dstats.getMax()+", Min:"+dstats.getMin());
System.out.println("Count:"+dstats.getCount()+", Sum:"+dstats.getSum());
System.out.println("Average:"+dstats.getAverage());
System.out.println("---LongSummaryStatistics---");
LongSummaryStatistics lstats= LongStream.of(51l,23l,53l,23l,35l).
collect(LongSummaryStatistics::new, LongSummaryStatistics::accept,
LongSummaryStatistics::combine);
System.out.println("Max:"+lstats.getMax()+", Min:"+lstats.getMin());
System.out.println("Count:"+lstats.getCount()+", Sum:"+lstats.getSum());
System.out.println("Average:"+lstats.getAverage());
System.out.println("---IntSummaryStatistics---");
IntSummaryStatistics istats= IntStream.of(51,22,50,27,35).
collect(IntSummaryStatistics::new, IntSummaryStatistics::accept,
IntSummaryStatistics::combine);
System.out.println("Max:"+istats.getMax()+", Min:"+istats.getMin());
System.out.println("Count:"+istats.getCount()+", Sum:"+istats.getSum());
System.out.println("Average:"+istats.getAverage());
}
}
11.2 with objects
public classSummaryStatisticsDemoWithObject {public static voidmain(String[] args) {
System.out.println("--DoubleSummaryStatistics--");
List list =Rectangle.getRectangle();
DoubleSummaryStatistics dstats=list.stream()
.collect(Collectors.summarizingDouble(Rectangle::getWidth));
System.out.println("Max:"+dstats.getMax()+", Min:"+dstats.getMin());
System.out.println("Count:"+dstats.getCount()+", Sum:"+dstats.getSum());
System.out.println("Average:"+dstats.getAverage());
System.out.println("--IntSummaryStatistics--");
list=Rectangle.getRectangle();
IntSummaryStatistics istats=list.stream()
.collect(Collectors.summarizingInt(Rectangle::getLength));
System.out.println("Max:"+istats.getMax()+", Min:"+istats.getMin());
System.out.println("Count:"+istats.getCount()+", Sum:"+istats.getSum());
System.out.println("Average:"+istats.getAverage());
System.out.println("--LongSummaryStatistics--");
list=Rectangle.getRectangle();
LongSummaryStatistics lstats=list.stream().
collect(Collectors.summarizingLong(Rectangle::getId));
System.out.println("Max:"+lstats.getMax()+", Min:"+lstats.getMin());
System.out.println("Count:"+lstats.getCount()+", Sum:"+lstats.getSum());
System.out.println("Average:"+lstats.getAverage());
}
}
12. Collectors.averagingDouble/Long/Int()
求平均值
12.1 averagingDouble
public classAveragingDoubleExample {public static voidmain(String[] args) {
List list = Arrays.asList(1,2,3,4);
Double result= list.stream().collect(Collectors.averagingDouble(d->d*2));
System.out.println(result);
}
}
12.2 averagingInt
public classAveragingIntExample {public static voidmain(String[] args) {
List list = Arrays.asList(1,2,3,4);
Double result= list.stream().collect(Collectors.averagingInt(v->v*2));
System.out.println(result);
}
}
12.3 averagingLong
public classAveragingLongExample {public static voidmain(String[] args) {
List list = Arrays.asList(1,2,3,4);
Double result= list.stream().collect(Collectors.averagingLong(v->v*2));
System.out.println(result);
}
}
13. maxBy/minBy
求最大值、最小值
public classMaxByMinBy {public static voidmain(String[] args) {
Student s1= new Student("Shyam", 22,"A");
Student s2= new Student("Ram",23,"A");
Student s3= new Student("Mohan",22,"B");
Student s4= new Student("Ramesh",21,"B");
List list =Arrays.asList(s1,s2,s3,s4);
Comparator ageComparator =Comparator.comparing(Student::getAge);//Using BinaryOperator.maxBy
System.out.println("---BinaryOperator.maxBy---");
Map> eldestByClass =list.stream().collect(Collectors.groupingBy(Student::getClassName,
Collectors.reducing(BinaryOperator.maxBy(ageComparator))));
eldestByClass.forEach((k,v)->System.out.println("Class:"+k+" Age:"+((Optional)v).get().getAge()+" Name:"+((Optional)v).get().getName()));//Using BinaryOperator.minBy
System.out.println("---BinaryOperator.minBy---");
Map> youngestByClass =list.stream().collect(Collectors.groupingBy(Student::getClassName,
Collectors.reducing(BinaryOperator.minBy(ageComparator))));
youngestByClass.forEach((k,v)->System.out.println("Class:"+k+" Age:"+((Optional)v).get().getAge()+" Name:"+((Optional)v).get().getName()));
}
}
有道词典
Here we will pa ...
X
在这里我们会通过BinaryOperator蓄电池。数字BinaryOperator,开始值是0。在字符串的情况下,开始值将是一个空字符串。