Collectors它是个工具类,提供了很多静态方法来返回Collector。通常作为Stream.collect()方法的入参,来实现更多的功能。
求平均值
static |
averagingDouble(ToDoubleFunction super T> mapper) Returns a |
static |
averagingInt(ToIntFunction super T> mapper) Returns a |
static |
averagingLong(ToLongFunction super T> mapper) Returns a |
统计
static |
counting() Returns a |
分组
static |
groupingBy(Function super T,? extends K> classifier) Returns a |
static |
groupingBy(Function super T,? extends K> classifier, Collector super T,A,D> downstream) Returns a |
static |
groupingBy(Function super T,? extends K> classifier, Supplier Returns a |
static |
groupingByConcurrent(Function super T,? extends K> classifier) Returns a concurrent |
static |
groupingByConcurrent(Function super T,? extends K> classifier, Collector super T,A,D> downstream) Returns a concurrent |
static |
groupingByConcurrent(Function super T,? extends K> classifier, Supplier Returns a concurrent |
字符串操作
static Collector |
joining() Returns a |
static Collector |
joining(CharSequence delimiter) Returns a |
static Collector |
joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) Returns a |
mapping
类似Stream.map()方法
static |
mapping(Function super T,? extends U> mapper, Collector super U,A,R> downstream) Adapts a |
最值
static |
maxBy(Comparator super T> comparator) Returns a |
static |
minBy(Comparator super T> comparator) Returns a |
分区
返回一个Map,key值为true或false,value为满足对应条件的数据。
static |
partitioningBy(Predicate super T> predicate) Returns a |
static |
partitioningBy(Predicate super T> predicate, Collector super T,A,D> downstream) Returns a |
规约
static |
reducing(BinaryOperator Returns a |
static |
reducing(T identity, BinaryOperator Returns a |
static |
reducing(U identity, Function super T,? extends U> mapper, BinaryOperator op) Returns a |
求和
static |
summingDouble(ToDoubleFunction super T> mapper) Returns a |
static |
summingInt(ToIntFunction super T> mapper) Returns a |
static |
summingLong(ToLongFunction super T> mapper) Returns a |
summarizing
返回各种SummarayStatistics对象,通过这些对象可以获取最大值、最小值、平均值、计数等。
static |
summarizingDouble(ToDoubleFunction super T> mapper) Returns a |
static |
summarizingInt(ToIntFunction super T> mapper) Returns a |
static |
summarizingLong(ToLongFunction super T> mapper) Returns a |
集合转换
可以将Stream流转换成List、Map、Set集合。
static |
toCollection(Supplier Returns a |
static |
toConcurrentMap(Function super T,? extends K> keyMapper, Function super T,? extends U> valueMapper) Returns a concurrent |
static |
toConcurrentMap(Function super T,? extends K> keyMapper, Function super T,? extends U> valueMapper, BinaryOperator mergeFunction) Returns a concurrent |
static |
toConcurrentMap(Function super T,? extends K> keyMapper, Function super T,? extends U> valueMapper, BinaryOperator mergeFunction, Supplier Returns a concurrent |
static |
toList() Returns a |
static |
toMap(Function super T,? extends K> keyMapper, Function super T,? extends U> valueMapper) Returns a |
static |
toMap(Function super T,? extends K> keyMapper, Function super T,? extends U> valueMapper, BinaryOperator mergeFunction) Returns a |
static |
toMap(Function super T,? extends K> keyMapper, Function super T,? extends U> valueMapper, BinaryOperator mergeFunction, Supplier Returns a |
static |
toSet() Returns a |
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class CollectorsTest2 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new Book("Core Java", 200,30));
list.add(new Book("Learning Freemarker", 150, 50));
list.add(new Book("Spring MVC", 300, 50));
// list.add(new Book("Spring MVC", 300, 50));
// averagingDouble方法
ToDoubleFunction toDoubleFunction = new ToDoubleFunction() {
@Override
public double applyAsDouble(Book book) {
return book.getPrice();
}
};
Double averagingDouble = list.stream().collect(Collectors.averagingDouble(toDoubleFunction));
System.out.println(averagingDouble);
// averagingInt
Double averagingInt = list.stream().collect(Collectors.averagingInt(Book::getCount));
System.out.println(averagingInt);
// averagingLong
Double averagingLong = list.stream().collect(Collectors.averagingLong(Book::getCount));
System.out.println(averagingLong);
// collectingAndThen
Integer andThen = list.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(Book::getName), map -> map.size()));
System.out.println(andThen);
// `counting()`
Long count = list.stream().collect(Collectors.counting());
System.out.println(count);
// groupingBy
Map> groupingBy = list.stream().collect(Collectors.groupingBy(Book::getName));
System.out.println(groupingBy); // {Learning Freemarker=[Book{name='Learning Freemarker', price=150.0, count=50}], Core Java=[Book{name='Core Java', price=200.0, count=30}], Spring MVC=[Book{name='Spring MVC', price=300.0, count=50}, Book{name='Spring MVC', price=300.0, count=50}]}
Map groupingBy_Reduce = list.stream().collect(Collectors.groupingBy(Book::getName, Collectors.counting()));
System.out.println(groupingBy_Reduce); // {Learning Freemarker=1, Core Java=1, Spring MVC=2}
HashMap groupingBy_ResultMap_Reduce = list.stream().collect(Collectors.groupingBy(Book::getName, HashMap::new, Collectors.counting()));
System.out.println(groupingBy_ResultMap_Reduce); // {Learning Freemarker=1, Core Java=1, Spring MVC=2}
// groupingByConcurrent
ConcurrentMap> concurrentGroupBy = list.stream().collect(Collectors.groupingByConcurrent(Book::getName));
ConcurrentMap concurrentGroupBy_Reduce = list.stream().collect(Collectors.groupingByConcurrent(Book::getName, Collectors.counting()));
ConcurrentHashMap concurrentGroupBy_ResultMap_Reduce = list.stream().collect(Collectors.groupingByConcurrent(Book::getName, ConcurrentHashMap::new, Collectors.counting()));
// joining
String joining = list.stream().map(Book::getName).collect(Collectors.joining(","));
System.out.println(joining); // Core Java,Learning Freemarker,Spring MVC,Spring MVC
// mapping
String mapping = list.stream().collect(Collectors.mapping(Book::getName, Collectors.joining(",")));
System.out.println(mapping); // Core Java,Learning Freemarker,Spring MVC,Spring MVC
// maxBy
Optional maxBy = list.stream().collect(Collectors.maxBy(Comparator.comparing(Book::getPrice)));
maxBy.ifPresent(System.out::println); // Book{name='Spring MVC', price=300.0, count=50}
// minBy
Optional minBy = list.stream().map(Book::getName).collect(Collectors.maxBy(String::compareTo));
minBy.ifPresent(System.out::println); // Spring MVC
// partitioningBy
Map> partitioningBy = list.stream().collect(Collectors.partitioningBy(book -> book.getPrice() > 200));
System.out.println(partitioningBy); // {false=[Book{name='Core Java', price=200.0, count=30}, Book{name='Learning Freemarker', price=150.0, count=50}], true=[Book{name='Spring MVC', price=300.0, count=50}, Book{name='Spring MVC', price=300.0, count=50}]}
Map partitioningBy_Reduce = list.stream().collect(Collectors.partitioningBy(book -> book.getPrice() > 200, Collectors.counting()));
System.out.println(partitioningBy_Reduce); // {false=2, true=2}
// reducing
Optional reducing = list.stream().collect(Collectors.reducing(BinaryOperator.maxBy(Comparator.comparing(Book::getPrice))));
reducing.ifPresent(System.out::println); // Book{name='Spring MVC', price=300.0, count=50}
// summarizing
IntSummaryStatistics intSummaryStatistics = list.stream().collect(Collectors.summarizingInt(Book::getCount));
double average = intSummaryStatistics.getAverage();
long count1 = intSummaryStatistics.getCount();
int max = intSummaryStatistics.getMax();
int min = intSummaryStatistics.getMin();
// toConcurrentMap
ConcurrentMap toConcurrentMap = list.stream().collect(Collectors.toConcurrentMap(Book::getName, Book::getCount));
System.out.println(toConcurrentMap);
// toList
List toList = list.stream().collect(Collectors.toList());
// toMap
Map toMap = list.stream().collect(Collectors.toMap(Book::getName, Book::getCount));
}
static class Book {
private String name;
private double price;
private int count;
public Book(String name, double price, int count) {
this.name = name;
this.price = price;
this.count = count;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
", count=" + count +
'}';
}
}
}
希望能帮助大家熟悉Collectors的使用。