java collector,
00-1010 I收集器II、工厂收集器2.1成为ConcurrentMap2.2成为Map2.3成为Collection2.4成为String2.5计算最大值2.6平均值2.7统计2.8求和2.9减函数2.10计数2.11分组-map2.12分组-ConcurrentMap2.13分流2.14收集器2.15收集后继续做一些处理。
00-1010//t:表示流中每个元素的类型。a:表示中间结果容器的类型。r:表示最终返回的结果类型。接口收集器T,a,r { supplier a supplier()//生成容器BiConsumerA,T accumulator()//是添加的元素BinaryOperatorA combiner()///是合并的容器FunctionA,Nisher()///是输出结果,Set Collector。Characteristics characteristics返回集的collector.characteristics表示该收集器的特征。//返回一个新的收集器,带有给定的提供者、累加器、合并器和完成器函数的说明。静态收集器(供应商,双消费者,累加器,双运算器,功能,整理器,收集器。特征.characteristics返回一个新的收集器,带有给定的提供者、累加器和组合器函数的描述。静态T,R收集器,R,R of(supplier supplier,BiConsumerR,T accumulator,BinaryOperatorR组合器,收集器。特征.特征)}
目录
公共最终类收集器扩展对象
Collector,作为Stream的collect方法的参数,是一个接口,是一个变量聚合操作,将输入元素累加到一个变量结果容器中;它会在处理完所有元素后将累积的结果转换成最终的表示(这是一个可选操作);
Collectors本身提供了一个关于Collectors的通用聚合实现。收集器的内部类CollectorImpl实现收集器接口。收藏家本身实际上是一个工厂。
00-1010//返回ConcurrentMap的并发函数,收集器元素在该函数中累积,其键和值是将提供的映射函数应用于输入元素的结果。静态T,K,U收集器,ConcurrentMapK,U toConcurrentMap(函数?超T,扩展K键映射器,函数?超T,extends U value mapper)///将收集器元素的累积返回给
其中 ConcurrentMap的并发函数,其键和值是将提供的映射函数应用于输入元素的结果。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)//返回将Collector元素累积到其中 ConcurrentMap的并发函数,其键和值是将提供的映射函数应用于输入元素的结果。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)//1、当key重复时,会抛出异常:java.lang.IllegalStateException: Duplicate key //2、当value为null时,会抛出异常:java.lang.NullPointerException
案例:
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);//{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<? 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);//{a=3, b=3, c=3, d=2, e=5}
//返回将Collector元素累积到 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);//[3, 3, 3, 2, 2, 3]System.out.println(list.getClass());//class java.util.ArrayListSet<Integer>set=integerList.stream().map(Person::getAge).collect(Collectors.toSet());System.out.println(set);//[2, 3]System.out.println(set.getClass());//class java.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());//class java.util.LinkedList
2.4 变成String
static Collector<CharSequence,?,String> joining()//delimiter分隔符连接static Collector<CharSequence,?,String> joining(CharSequence delimiter) //prefix前缀//suffix后缀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);//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)
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());//Person{name=e,age=6}
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);//1.0
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)//将另一个的状态合并IntSummaryStatistics到这个状态中。double getAverage()//算术平均值,如果没有记录值,则返回零。long getCount()//返回记录的值的计数。int getMax()//返回记录的最大值,或者Integer.MIN_VALUE没有记录值。int getMin()//返回记录的最小值,或者Integer.MAX_VALUE没有记录值。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());//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.0
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函数
//op 缩减的函数static <T> Collector<T,?,Optional<T>> reducing(BinaryOperator<T> op) //identity储存器初始值static <T> Collector<T,?,T> reducing(T identity, BinaryOperator<T> op)//mapper作用的数值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
2.10 计数
//返回Collector类型的接受元素,T用于计算输入元素的数量。static <T> Collector<T,?,Long> counting()
2.11 分组-变成map
//classifier分组依据函数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}]}
//downstream将小组内对象进行处理static <T,K,A,D> Collector<T,?,Map<K,D>> groupingBy(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream)//mapFactory中间操作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);//{a=6, b=15}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());//classjava.util.TreeMap
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 分割流
//predicate分区的依据static <T> Collector<T,?,Map<Boolea
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。