1. <nobr id="easjo"><address id="easjo"></address></nobr>

      <track id="easjo"><source id="easjo"></source></track>
      1. 
        

      2. <bdo id="easjo"><optgroup id="easjo"></optgroup></bdo>
      3. <track id="easjo"><source id="easjo"><em id="easjo"></em></source></track><option id="easjo"><span id="easjo"><em id="easjo"></em></span></option>
          貴州做網站公司
          貴州做網站公司~專業!靠譜!
          10年網站模板開發經驗,熟悉國內外開源網站程序,包括DEDECMS,WordPress,ZBlog,Discuz! 等網站程序,可為您提供網站建設,網站克隆,仿站,網頁設計,網站制作,網站推廣優化等服務。我們專注高端營銷型網站,企業官網,集團官網,自適應網站,手機網站,網絡營銷,網站優化,網站服務器環境搭建以及托管運維等。為客戶提供一站式網站解決方案?。?!

          collector(java收集器Collector怎么使用)

          來源:互聯網轉載 時間:2024-05-12 16:50:29

          一、收集器Collector

          //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)}

          二、收集器工廠Collectors

          public final class Collectors extends Object

          Collectors作為Stream的collect方法的參數,Collector是一個接口,它是一個可變的匯聚操作,將輸入元素累計到一個可變的結果容器中;它會在所有元素都處理完畢后,將累積的結果轉換為一個最終的表示(這是一個可選操作);

          Collectors本身提供了關于Collector的常見匯聚實現,Collectors的內部類CollectorImpl實現了Collector接口,Collectors本身實際上是一個
          工廠。

          2.1 變成ConcurrentMap

          //返回將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)

          2.2 變成Map

          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)

          2.3 變成Collection

          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.LinkedList

          2.4 變成String

          staticCollector<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)

          2.5 計算最值

          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'}

          2.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.0

          2.7 統計數據

          static<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.0

          2.8 求和

          static<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)

          2.9 reducing函數

          //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

          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<?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.TreeMap

          2.12 分組-變成ConcurrentMap

          static<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)

          2.13 分割流

          //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)

          2.14 收集器

          通過在累積之前將映射函數應用于每個輸入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怎么使用”有了更深的了解,不妨來實際操作一番吧!這里是本站網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

          標簽:collector-

          c語言中正確的字符常量是用一對單引號將一個字符括起表示合法的字符常量。例如‘a’。數值包括整型、浮點型。整型可用十進制,八進制,十六進制。八進制前面要加0,后面...

          2022年天津專場考試原定于3月19日舉行,受疫情影響確定延期,但目前延期后的考試時間推遲。 符合報名條件的考生,須在規定時間登錄招考資訊網(www.zha...

          :喜歡聽,樂意看。指很受歡迎?!巴卣官Y料”喜聞樂見:[ xǐ wén lè jiàn ]詳細解釋1. 【解釋】:喜歡聽,樂意看。指很受歡迎。2. 【示例】:這是...

          最近小編看到很多人在搜索K寶的相關內容,小編呢對此也是非常感興趣,特意整理了相關的內容,下面就和小編一起來看下吧!k寶密碼忘了怎么辦?去銀行重置k寶的密碼:k寶密碼有6次輸入的機會,如果輸錯則會被鎖定,K寶密碼忘記需要卡主本人帶開卡證件及銀行卡到就近農業銀行任意柜臺辦理補辦證書業務即可。k寶密碼忘了能不能在網上修改?K寶密碼忘記了需要卡主本人帶好身份證和銀行卡去附近的銀行辦理密碼重置手續,手續費是...

          國債期貨代碼有哪些?國債期貨代碼(兩年期國債期貨代碼):TS2003上市交易所:大連商品交易所國債期貨代碼:030011上市交易所:上海期貨交易所交易品種:國債交易代碼:TS2003發行上市交易所:上海期貨交易所國債期貨的標的是國內最早推出的國債期貨合約,2005年11月10日正式掛牌交易,這也是國債期貨首個允許境內客戶參與的品種,是目前最受歡迎的國債期貨。國債期貨理論價格計算公式計算公式有兩種:...

          德馬克指標是什么意思?托馬斯德馬克(德馬克)是道明系列指標的創始人。TD指標之間約有70種,其中一個應用最廣泛的是TD序列和TD組合。TD序列由價格反轉、TD結構、TD計數三部分組成。TD組合和TD序列基本類似,只是計數部分有些區別。相對來說,TD組合的計數條件有一定的差異。相對來說,TD組合的計數條件更為嚴格。TD趨勢線有兩種:一種是TD買入結構的TD趨勢阻力線;一種是TD賣出結構的TD趨勢支撐...

          TOP
          国产初高中生视频在线观看|亚洲一区中文|久久亚洲欧美国产精品|黄色网站入口免费进人
          1. <nobr id="easjo"><address id="easjo"></address></nobr>

              <track id="easjo"><source id="easjo"></source></track>
              1. 
                

              2. <bdo id="easjo"><optgroup id="easjo"></optgroup></bdo>
              3. <track id="easjo"><source id="easjo"><em id="easjo"></em></source></track><option id="easjo"><span id="easjo"><em id="easjo"></em></span></option>