发布时间:2025-12-10 23:08:24 浏览次数:1
//T:表示流中每个元素的类型。A:表示中间结果容器的类型。R:表示最终返回的结果类型。publicinterfaceCollector<T,A,R>{Supplier<A>supplier()//生成容器BiConsumer<A,T>accumulator()//是添加元素BinaryOperator<A>combiner()//是合并容器Function<A,R>finisher()///是输出的结果Set<Collector.Characteristics>characteristics()//返回Set的Collector.Characteristics指示此收集器的特征。//返回一个新的Collector由给定的描述supplier,accumulator,combiner,和finisher功能。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)//返回一个新的Collector由给定的描述supplier,accumulator和combiner功能。static<T,R>Collector<T,R,R>of(Supplier<R>supplier,BiConsumer<R,T>accumulator,BinaryOperator<R>combiner,Collector.Characteristics...characteristics)}public final class Collectors extends Object
Collectors作为Stream的collect方法的参数,Collector是一个接口,它是一个可变的汇聚操作,将输入元素累计到一个可变的结果容器中;它会在所有元素都处理完毕后,将累积的结果转换为一个最终的表示(这是一个可选操作);
Collectors本身提供了关于Collector的常见汇聚实现,Collectors的内部类CollectorImpl实现了Collector接口,Collectors本身实际上是一个
工厂。
//返回将Collector元素累积到其中ConcurrentMap的并发函数,其键和值是将提供的映射函数应用于输入元素的结果。static<T,K,U>Collector<T,?,ConcurrentMap<K,U>>toConcurrentMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper)//返回将Collector元素累积到其中ConcurrentMap的并发函数,其键和值是将提供的映射函数应用于输入元素的结果。static<T,K,U>Collector<T,?,ConcurrentMap<K,U>>toConcurrentMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper,BinaryOperator<U>mergeFunction)//返回将Collector元素累积到其中ConcurrentMap的并发函数,其键和值是将提供的映射函数应用于输入元素的结果。static<T,K,U,MextendsConcurrentMap<K,U>>Collector<T,?,M>toConcurrentMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper,BinaryOperator<U>mergeFunction,Supplier<M>mapSupplier)
static<T,K,U>Collector<T,?,Map<K,U>>toMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper)//1、当key重复时,会抛出异常:java.lang.IllegalStateException:Duplicatekey//2、当value为null时,会抛出异常:java.lang.NullPointerException
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",3));integerList.add(newPerson("b",3));integerList.add(newPerson("c",3));integerList.add(newPerson("d",2));integerList.add(newPerson("e",2));integerList.add(newPerson("f",2));Mapmap=integerList.stream().collect(Collectors.toMap(Person::getName,Person::getAge));System.out.println(map);//{a=3,b=3,c=3,d=2,e=2,f=2}//第三个参数用在key值冲突的情况下:如果新元素产生的key在Map中已经出现过了,第三个参数就会定义解决的办法。static<T,K,U>Collector<T,?,Map<K,U>>toMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper,BinaryOperator<U>mergeFunction)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",3));integerList.add(newPerson("b",3));integerList.add(newPerson("c",3));integerList.add(newPerson("d",2));integerList.add(newPerson("e",2));integerList.add(newPerson("e",3));Collections.sort(integerList,comparator);System.out.println(integerList);*/Mapmap=integerList.stream().collect(Collectors.toMap(Person::getName,Person::getAge,(a,b)->a+b));System.out.println(map);//{a=3,b=3,c=3,d=2,e=5}//返回将Collector元素累积到Map其键中的值,其值是将提供的映射函数应用于输入元素的结果。static<T,K,U,MextendsMap<K,U>>Collector<T,?,M>toMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper,BinaryOperator<U>mergeFunction,Supplier<M>mapSupplier)
static<T>Collector<T,?,List<T>>toList()static<T>Collector<T,?,Set<T>>toSet()//自定义static<T,CextendsCollection<T>>Collector<T,?,C>toCollection(Supplier<C>collectionFactory)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",3));integerList.add(newPerson("b",3));integerList.add(newPerson("c",3));integerList.add(newPerson("d",2));integerList.add(newPerson("e",2));integerList.add(newPerson("e",3));List<Integer>list=integerList.stream().map(Person::getAge).collect(Collectors.toList());System.out.println(list);//[3,3,3,2,2,3]System.out.println(list.getClass());//classjava.util.ArrayListSet<Integer>set=integerList.stream().map(Person::getAge).collect(Collectors.toSet());System.out.println(set);//[2,3]System.out.println(set.getClass());//classjava.util.HashSetLinkedList<Integer>linkedList=integerList.stream().map(Person::getAge).collect(Collectors.toCollection(LinkedList::new));System.out.println(linkedList);//[3,3,3,2,2,3]System.out.println(linkedList.getClass());//classjava.util.LinkedListstaticCollector<CharSequence,?,String>joining()//delimiter分隔符连接staticCollector<CharSequence,?,String>joining(CharSequencedelimiter)//prefix前缀//suffix后缀staticCollector<CharSequence,?,String>joining(CharSequencedelimiter,CharSequenceprefix,CharSequencesuffix)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",3));integerList.add(newPerson("b",3));integerList.add(newPerson("c",3));integerList.add(newPerson("d",2));integerList.add(newPerson("e",2));integerList.add(newPerson("e",3));Stringlist=integerList.stream().map(Person::getName).collect(Collectors.joining());System.out.println(list);//abcdeeStringset=integerList.stream().map(Person::getName).collect(Collectors.joining(","));System.out.println(set);//a,b,c,d,e,eStringlinkedList=integerList.stream().map(Person::getName).collect(Collectors.joining(",","(",")"));System.out.println(linkedList);//(a,b,c,d,e,e)static<T>Collector<T,?,Optional<T>>maxBy(Comparator<?superT>comparator)static<T>Collector<T,?,Optional<T>>minBy(Comparator<?superT>comparator)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",1));integerList.add(newPerson("b",2));integerList.add(newPerson("c",3));integerList.add(newPerson("d",4));integerList.add(newPerson("e",5));integerList.add(newPerson("e",6));Optional<Person>person=integerList.stream().collect(Collectors.maxBy(Comparator.comparing(Person::getAge)));System.out.println(person.get());//Person{name='e',age='6'}static<T>Collector<T,?,Double>averagingDouble(ToDoubleFunction<?superT>mapper)static<T>Collector<T,?,Double>averagingInt(ToIntFunction<?superT>mapper)static<T>Collector<T,?,Double>averagingLong(ToLongFunction<?superT>mapper)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",1));integerList.add(newPerson("b",1));integerList.add(newPerson("c",1));integerList.add(newPerson("d",1));integerList.add(newPerson("e",1));integerList.add(newPerson("e",1));doublenumber=integerList.stream().collect(Collectors.averagingDouble(Person::getAge));System.out.println(number);//1.0static<T>Collector<T,?,DoubleSummaryStatistics>summarizingDouble(ToDoubleFunction<?superT>mapper)static<T>Collector<T,?,IntSummaryStatistics>summarizingInt(ToIntFunction<?superT>mapper)static<T>Collector<T,?,LongSummaryStatistics>summarizingLong(ToLongFunction<?superT>mapper)
DoubleSummaryStatistics,IntSummaryStatistics,LongSummaryStatistics 用于收集统计数据(如计数,最小值,最大值,总和和平均值)的状态对象。
此实现不是线程安全的。但是,Collectors.toXXXStatistics()在并行流上使用是安全的 ,因为并行实现Stream.collect() 提供了必要的分区,隔离和合并结果,以实现安全有效的并行执行。
他们的方法如下:
voidaccept(intvalue)//添加一个值voidcombine(IntSummaryStatisticsother)//将另一个的状态合并IntSummaryStatistics到这个状态中。doublegetAverage()//算术平均值,如果没有记录值,则返回零。longgetCount()//返回记录的值的计数。intgetMax()//返回记录的最大值,或者Integer.MIN_VALUE没有记录值。intgetMin()//返回记录的最小值,或者Integer.MAX_VALUE没有记录值。longgetSum()//返回记录的值的总和,如果没有记录值,则返回零。StringtoString()//返回对象的字符串表示形式。
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",1));integerList.add(newPerson("b",2));integerList.add(newPerson("c",3));integerList.add(newPerson("d",4));integerList.add(newPerson("e",5));integerList.add(newPerson("e",6));DoubleSummaryStatisticsnumber=integerList.stream().collect(Collectors.summarizingDouble(Person::getAge));System.out.println(number.getMax());//6System.out.println(number.getMin());//1.0System.out.println(number.getSum());//21.0System.out.println(number.getAverage());//3.5number.accept(100);System.out.println(number.getMax());//100.0static<T>Collector<T,?,Double>summingDouble(ToDoubleFunction<?superT>mapper)static<T>Collector<T,?,Integer>summingInt(ToIntFunction<?superT>mapper)static<T>Collector<T,?,Long>summingLong(ToLongFunction<?superT>mapper)
//op缩减的函数static<T>Collector<T,?,Optional<T>>reducing(BinaryOperator<T>op)//identity储存器初始值static<T>Collector<T,?,T>reducing(Tidentity,BinaryOperator<T>op)//mapper作用的数值static<T,U>Collector<T,?,U>reducing(Uidentity,Function<?superT,?extendsU>mapper,BinaryOperator<U>op)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",1));integerList.add(newPerson("b",0));integerList.add(newPerson("c",0));integerList.add(newPerson("d",0));integerList.add(newPerson("e",0));integerList.add(newPerson("e",0));Integernumber=integerList.stream().collect(Collectors.reducing(1,Person::getAge,(a,b)->a+b));System.out.println(number);//2//返回Collector类型的接受元素,T用于计算输入元素的数量。static<T>Collector<T,?,Long>counting()
//classifier分组依据函数static<T,K>Collector<T,?,Map<K,List<T>>>groupingBy(Function<?superT,?extendsK>classifier)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",1));integerList.add(newPerson("a",2));integerList.add(newPerson("a",3));integerList.add(newPerson("b",4));integerList.add(newPerson("b",5));integerList.add(newPerson("b",6));Mapmap=integerList.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'}]}//downstream将小组内对象进行处理static<T,K,A,D>Collector<T,?,Map<K,D>>groupingBy(Function<?superT,?extendsK>classifier,Collector<?superT,A,D>downstream)//mapFactory中间操作static<T,K,D,A,MextendsMap<K,D>>Collector<T,?,M>groupingBy(Function<?superT,?extendsK>classifier,Supplier<M>mapFactory,Collector<?superT,A,D>downstream)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",1));integerList.add(newPerson("a",2));integerList.add(newPerson("a",3));integerList.add(newPerson("b",4));integerList.add(newPerson("b",5));integerList.add(newPerson("b",6));Mapmap=integerList.stream().collect(Collectors.groupingBy(Person::getName,Collectors.reducing(0,Person::getAge,(a,b)->a+b)));System.out.println(map);//{a=6,b=15}Mapmap=integerList.stream().collect(Collectors.groupingBy(Person::getName,TreeMap::new,Collectors.reducing(0,Person::getAge,(a,b)->a+b)));System.out.println(map.getClass());//classjava.util.TreeMapstatic<T,K>Collector<T,?,ConcurrentMap<K,List<T>>>groupingByConcurrent(Function<?superT,?extendsK>classifier)static<T,K,A,D>Collector<T,?,ConcurrentMap<K,D>>groupingByConcurrent(Function<?superT,?extendsK>classifier,Collector<?superT,A,D>downstream)static<T,K,A,D,MextendsConcurrentMap<K,D>>Collector<T,?,M>groupingByConcurrent(Function<?superT,?extendsK>classifier,Supplier<M>mapFactory,Collector<?superT,A,D>downstream)
//predicate分区的依据static<T>Collector<T,?,Map<Boolean,List<T>>>partitioningBy(Predicate<?superT>predicate)static<T,D,A>Collector<T,?,Map<Boolean,D>>partitioningBy(Predicate<?superT>predicate,Collector<?superT,A,D>downstream)
通过在累积之前将映射函数应用于每个输入Collector元素,使类型的接受元素适应一个接受类型的U元素T。
static<T,U,A,R>Collector<T,?,R>mapping(Function<?superT,?extendsU>mapper,Collector<?superU,A,R>downstream)
案例:
List<Person>integerList=newArrayList<>();integerList.add(newPerson("a",1));integerList.add(newPerson("a",2));integerList.add(newPerson("a",3));integerList.add(newPerson("b",4));integerList.add(newPerson("b",5));integerList.add(newPerson("b",6));Listlist=integerList.stream().collect(Collectors.mapping(Person::getName,Collectors.toList()));System.out.println(list);//[a,a,a,b,b,b]2.15 收集之后继续做一些处理
static<T,A,R,RR>Collector<T,A,RR>collectingAndThen(Collector<T,A,R>downstream,Function<R,RR>finisher)
到此,相信大家对“java收集器Collector怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是本站网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!