一、收集器Collector
public interface Collector<T, A, R> {
Supplier<A> supplier()
BiConsumer<A,T> accumulator()
BinaryOperator<A> combiner()
Function<A,R>finisher()
Set<Collector.Characteristics> characteristics()
static <T,A,R> Collector<T,A,R> of(Supplier<A> supplier,
BiConsumer<A,T> accumulator,
BinaryOperator<A> combiner,
Function<A,R> finisher,
Collector.Characteristics... characteristics)
static <T,R> Collector<T,R,R> of(Supplier<R> supplier,
BiConsumer<R,T> accumulator,
BinaryOperator<R> combiner,
Collector.Characteristics... characteristics)
}
二、收集器工厂Collectors
public final class Collectors extends Object
Collectors作为Stream的collect方法的参数,Collector是一个接口,它是一个可变的汇聚操作,将输入元素累计到一个可变的结果容器中;它会在所有元素都处理完毕后,将累积的结果转换为一个最终的表示(这是一个可选操作);
Collectors本身提供了关于Collector的常见汇聚实现,Collectors的内部类CollectorImpl实现了Collector接口,Collectors本身实际上是一个 工厂。
2.1 变成ConcurrentMap
static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper)
static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction)
static <T,K,U,M extends ConcurrentMap<K,U>> Collector<T,?,M> toConcurrentMap(
Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier
)
2.2 变成Map
static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper)
案例
List<Person>integerList=newArrayList<>();
integerList.add(new Person("a",3));
integerList.add(new Person("b",3));
integerList.add(new Person("c",3));
integerList.add(new Person("d",2));
integerList.add(new Person("e",2));
integerList.add(new Person("f",2));
Mapmap=integerList.stream().collect(Collectors.toMap(Person::getName,Person::getAge));
System.out.println(map);
static <T,K,U> Collector<T,?,Map<K,U>> toMap( Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction)
案例
List<Person> integerList = new ArrayList<>();
integerList.add(new Person("a",3));
integerList.add(new Person("b",3));
integerList.add(new Person("c",3));
integerList.add(new Person("d",2));
integerList.add(new Person("e",2));
integerList.add(new Person("e",3));
Collections.sort(integerList,comparator);
System.out.println(integerList);*/
Map map =integerList.stream().collect(Collectors.toMap(Person::getName,Person::getAge,(a,b)->a+b));
System.out.println(map);
static <T,K,U,M extends Map<K,U>> Collector<T,?,M> toMap( Function<? super T,? extends K> keyMapper,
Function<? super T,? extends U> valueMapper,
BinaryOperator<U> mergeFunction,
Supplier<M> mapSupplier)
2.3 变成Collection
static <T> Collector<T,?,List<T>> toList()
static <T> Collector<T,?,Set<T>> toSet()
static <T,C extends Collection<T>> Collector<T,?,C> toCollection(Supplier<C> collectionFactory)
案例
List<Person> integerList = new ArrayList<>();
integerList.add(new Person("a",3));
integerList.add(new Person("b",3));
integerList.add(new Person("c",3));
integerList.add(new Person("d",2));
integerList.add(new Person("e",2));
integerList.add(new Person("e",3));
List<Integer> list= integerList.stream().map(Person::getAge).collect(Collectors.toList());
System.out.println(list);
System.out.println(list.getClass());
Set<Integer>set=integerList.stream().map(Person::getAge).collect(Collectors.toSet());
System.out.println(set);
System.out.println(set.getClass());
LinkedList<Integer>linkedList=integerList.stream().map(Person::getAge).collect(Collectors.toCollection(LinkedList::new));
System.out.println(linkedList);
System.out.println(linkedList.getClass());
2.4 变成String
static Collector<CharSequence,?,String> joining()
static Collector<CharSequence,?,String> joining(CharSequence delimiter)
static Collector<CharSequence,?,String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
案例
List<Person> integerList = newArrayList<>();
integerList.add(new Person("a",3));
integerList.add(new Person("b",3));
integerList.add(new Person("c",3));
integerList.add(new Person("d",2));
integerList.add(new Person("e",2));
integerList.add(new Person("e",3));
Stringlist = integerList.stream().map(Person::getName).collect(Collectors.joining());
System.out.println(list);
Stringset = integerList.stream().map(Person::getName).collect(Collectors.joining(","));
System.out.println(set);
StringlinkedList = integerList.stream().map(Person::getName).collect(Collectors.joining(",","(",")"));
System.out.println(linkedList);
2.5 计算最值
static <T> Collector<T,?,Optional<T>> maxBy(Comparator<? super T> comparator)
static <T> Collector<T,?,Optional<T>> minBy(Comparator<? super T> comparator)
案例
List<Person> integerList = new ArrayList<>();
integerList.add(new Person("a",1));
integerList.add(new Person("b",2));
integerList.add(new Person("c",3));
integerList.add(new Person("d",4));
integerList.add(new Person("e",5));
integerList.add(new Person("e",6));
Optional<Person> person = integerList.stream().collect(Collectors.maxBy(Comparator.comparing(Person::getAge)));
System.out.println(person.get());
2.6 平均值
static <T> Collector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper)
static <T> Collector<T,?,Double> averagingInt(ToIntFunction<? super T> mapper)
static <T> Collector<T,?,Double> averagingLong(ToLongFunction<? super T> mapper)
案例
List<Person> integerList = new ArrayList<>();
integerList.add(new Person("a",1));
integerList.add(new Person("b",1));
integerList.add(new Person("c",1));
integerList.add(new Person("d",1));
integerList.add(new Person("e",1));
integerList.add(new Person("e",1));
double number=integerList.stream().collect(Collectors.averagingDouble(Person::getAge));
System.out.println(number);
2.7 统计数据
static <T> Collector<T,?,DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper)
static <T> Collector<T,?,IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper)
static <T> Collector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper)
DoubleSummaryStatistics,IntSummaryStatistics,LongSummaryStatistics 用于收集统计数据(如计数,最小值,最大值,总和和平均值)的状态对象。 此实现不是线程安全的。但是,Collectors.toXXXStatistics()在并行流上使用是安全的 ,因为并行实现Stream.collect() 提供了必要的分区,隔离和合并结果,以实现安全有效的并行执行。
他们的方法如下
void accept(int value)
void combine(IntSummaryStatistics other)
double getAverage()
long getCount()
int getMax()
int getMin()
long getSum()
String toString()
案例
List<Person> integerList = new ArrayList<>();
integerList.add(new Person("a",1));
integerList.add(new Person("b",2));
integerList.add(new Person("c",3));
integerList.add(new Person("d",4));
integerList.add(new Person("e",5));
integerList.add(new Person("e",6));
DoubleSummaryStatistics number = integerList.stream().collect(Collectors.summarizingDouble(Person::getAge));
System.out.println(number.getMax());
System.out.println(number.getMin());
System.out.println(number.getSum());
System.out.println(number.getAverage());
number.accept(100);
System.out.println(number.getMax());
2.8 求和
static <T> Collector<T,?,Double> summingDouble(ToDoubleFunction<? super T> mapper)
static <T> Collector<T,?,Integer> summingInt(ToIntFunction<? super T> mapper)
static <T> Collector<T,?,Long> summingLong(ToLongFunction<? super T> mapper)
2.9 reducing函数
static <T> Collector<T,?,Optional<T>> reducing(BinaryOperator<T> op)
static <T> Collector<T,?,T> reducing(T identity, BinaryOperator<T> op)
static <T,U> Collector<T,?,U> reducing(U identity, Function<? super T,? extends U> mapper, BinaryOperator<U> op)
案例
List<Person> integerList = new ArrayList<>();
integerList.add(new Person("a",1));
integerList.add(new Person("b",0));
integerList.add(new Person("c",0));
integerList.add(new Person("d",0));
integerList.add(new Person("e",0));
integerList.add(new Person("e",0));
Integernumber = integerList.stream().collect(Collectors.reducing(1,Person::getAge,(a,b)->a+b));
System.out.println(number);
2.10 计数
static <T> Collector<T,?,Long> counting()
2.11 分组-变成map
static <T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier)
案例
List<Person> integerList = new ArrayList<>();
integerList.add(new Person("a",1));
integerList.add(new Person("a",2));
integerList.add(new Person("a",3));
integerList.add(new Person("b",4));
integerList.add(new Person("b",5));
integerList.add(new Person("b",6));
Map map =i ntegerList.stream().collect(Collectors.groupingBy(Person::getName));
System.out.println(map);
{
a=[Person{name='a', age='1'}, Person{name='a', age='2'}, Person{name='a', age='3'}],
b=[Person{name='b', age='4'}, Person{name='b', age='5'}, Person{name='b', age='6'}]
}
static <T,K,A,D> Collector<T,?,Map<K,D>> groupingBy(Function<? super T,? extends K> classifier,
Collector<? super T,A,D> downstream)
static <T,K,D,A,M extends Map<K,D>> Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T,A,D> downstream)
案例
List<Person> integerList = newArrayList<>();
integerList.add(new Person("a",1));
integerList.add(new Person("a",2));
integerList.add(new Person("a",3));
integerList.add(new Person("b",4));
integerList.add(new Person("b",5));
integerList.add(new Person("b",6));
Map map= i ntegerList.stream()
.collect(Collectors.groupingBy(Person::getName,Collectors.reducing(0,Person::getAge,(a,b)->a+b)));
System.out.println(map);
Map map = integerList.stream()
.collect(Collectors.groupingBy(Person::getName,TreeMap::new,Collectors.reducing(0,Person::getAge,(a,b)->a+b)));
System.out.println(map.getClass());
2.12 分组-变成ConcurrentMap
static <T,K> Collector<T,?,ConcurrentMap<K,List<T>>> groupingByConcurrent(Function<? super T,? extends K> classifier)
static <T,K,A,D> Collector<T,?,ConcurrentMap<K,D>> groupingByConcurrent(Function<? super T,? extends K> classifier,
Collector<? super T,A,D> downstream)
static <T,K,A,D,M extends ConcurrentMap<K,D>> Collector<T,?,M> groupingByConcurrent(Function<? super T,? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T,A,D> downstream)
2.13 分割流
static <T> Collector<T,?,Map<Boolean,List<T>>> partitioningBy(Predicate<? super T> predicate)
static <T,D,A> Collector<T,?,Map<Boolean,D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T,A,D> downstream)
2.14 收集器
static <T,U,A,R> Collector<T,?,R> mapping(Function<? super T,? extends U> mapper, Collector<? super U,A,R> downstream)
案例
List<Person> integerList = new ArrayList<>();
integerList.add(new Person("a",1));
integerList.add(new Person("a",2));
integerList.add(new Person("a",3));
integerList.add(new Person("b",4));
integerList.add(new Person("b",5));
integerList.add(new Person("b",6));
List list = integerList.stream().collect(Collectors.mapping(Person::getName,Collectors.toList()));
System.out.println(list);
2.15 收集之后继续做一些处理
static <T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher)
|