国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - JDK新特性——Stream代碼簡(jiǎn)潔之道

JDK新特性——Stream代碼簡(jiǎn)潔之道

2021-05-06 23:02牧小農(nóng) Java教程

Stream 是 Java8 中處理集合的關(guān)鍵抽象概念,它可以指定你希望對(duì)集合進(jìn)行的操作,可以執(zhí)行非常復(fù)雜的查找、過(guò)濾和映射數(shù)據(jù)等操作.

JDK新特性——Stream代碼簡(jiǎn)潔之道

一、概述

 

Stream 是一組用來(lái)處理數(shù)組、集合的API,Stream API 提供了一種高效且易于使用的處理數(shù)據(jù)的方式。Java 8 中之所以費(fèi)這么大的功夫引入 函數(shù)式編程 ,原因有兩個(gè):

代碼簡(jiǎn)潔函數(shù)式編程寫(xiě)出的代碼簡(jiǎn)潔且意圖明確,使用stream接口讓你從此告別for循環(huán)。

多核友好,Java函數(shù)式編程使得編寫(xiě)并行程序從未如此簡(jiǎn)單,你需要的全部就是用用一下parallel()方法

Stream 是 Java8 中處理集合的關(guān)鍵抽象概念,它可以指定你希望對(duì)集合進(jìn)行的操作,可以執(zhí)行非常復(fù)雜的查找、過(guò)濾和映射數(shù)據(jù)等操作

二、Stream特性

 

1、不是數(shù)據(jù)結(jié)構(gòu),沒(méi)有內(nèi)部存儲(chǔ),不會(huì)保存數(shù)據(jù),故每個(gè)Stream流只能使用一次 2、不支持索引訪問(wèn) 3、支持并行 4、很容易生成數(shù)據(jù)或集合(List,Set) 5、支持過(guò)濾、查找、轉(zhuǎn)換、匯總、聚合等操作 6、延遲計(jì)算,流在中間處理過(guò)程中,只是對(duì)操作進(jìn)行了記錄,并不會(huì)立即執(zhí)行,需要等到執(zhí)行終止操作的時(shí)候才會(huì)進(jìn)行實(shí)際的計(jì)算

三、分類

 

關(guān)于應(yīng)用在Stream流上的操作,可以分成兩種:

  1. Intermediate(中間操作): 中間操作的返回結(jié)果都是Stream,故可以多個(gè)中間操作疊加;
  2. Terminal(終止操作): 終止操作用于返回我們最終需要的數(shù)據(jù),只能有一個(gè)終止操作。

使用Stream流,可以清楚地知道我們要對(duì)一個(gè)數(shù)據(jù)集做何種操作,可讀性強(qiáng)。而且可以很輕松地獲取并行化Stream流,不用自己編寫(xiě)多線程代碼,可以讓我們更加專注于業(yè)務(wù)邏輯。

JDK新特性——Stream代碼簡(jiǎn)潔之道

無(wú)狀態(tài): 指元素的處理不受之前元素的影響;有狀態(tài): 指該操作只有拿到所有元素之后才能繼續(xù)下去。非短路操作: 指必須處理所有元素才能得到最終結(jié)果;短路操作: 指遇到某些符合條件的元素就可以得到最終結(jié)果,如 A || B,只要A為true,則無(wú)需判斷B的結(jié)果。

四、Stream的創(chuàng)建

 

1、通過(guò)數(shù)組來(lái)生成 2、通過(guò)集合來(lái)生成 3、通過(guò)Stream.generate方法來(lái)創(chuàng)建 4、通過(guò)Stream.iterate方法來(lái)創(chuàng)建 5、其他Api創(chuàng)建

4.1 通過(guò)數(shù)組來(lái)生成

  1. //通過(guò)數(shù)組來(lái)生成 
  2.    static void gen1(){ 
  3.        String[] strs = {"a","b","c","d"}; 
  4.        Stream<String> strs1 = Stream.of(strs);//使用Stream中的靜態(tài)方法:of() 
  5.        strs1.forEach(System.out::println);//打印輸出(a、b、c、d) 
  6.    } 

4.2 通過(guò)集合來(lái)生成

  1. //通過(guò)集合來(lái)生成 
  2.     static void gen2(){ 
  3.         List<String> list = Arrays.asList("1","2","3","4"); 
  4.         Stream<String> stream = list.stream();//獲取一個(gè)順序流 
  5.         stream.forEach(System.out::println);//打印輸出(1,2,3,4) 
  6.     } 

4.3 通過(guò)Stream.generate方法來(lái)創(chuàng)建

  1. //generate 
  2. static void gen3(){ 
  3.     Stream<Integer> generate = Stream.generate(() -> 1);//使用Stream中的靜態(tài)方法:generate() 
  4.     //limit 返回由該流的元素組成的流,截?cái)嚅L(zhǎng)度不能超過(guò)maxSize 
  5.     generate.limit(10).forEach(System.out::println);//打印輸出(打印10個(gè)1) 

4.4 通過(guò)Stream.iterate方法來(lái)創(chuàng)建

  1. //使用iterator 
  2. static void gen4() { 
  3.     Stream<Integer> iterate = Stream.iterate(1, x -> x + 1);//使用Stream中的靜態(tài)方法:iterate() 
  4.     iterate.limit(10).forEach(System.out::println);//打印輸出(1,2,3,4,5,6,7,8,9,10) 

4.5其他Api創(chuàng)建

  1. //其他方式 
  2.     static void gen5(){ 
  3.         String str = "abcdefg"
  4.         IntStream stream =str.chars();//獲取str 字節(jié)碼 
  5.         stream.forEach(System.out::println);//打印輸出(97,98,99,100,101,102,103) 
  6.     } 

五、Stream的常用API

 

5.1 中間操作

1. filter:過(guò)濾流中的某些元素

  1. //中間操作:如果調(diào)用方法之后返回的結(jié)果是Stream對(duì)象就意味著是一個(gè)中間操作 
  2.  Arrays.asList(1,2,3,4,5).stream()//獲取順序流 
  3.  .filter((x)->x%2==0) // 2 4  
  4.  .forEach(System.out::println); 
  5.  
  6. //求出結(jié)果集中所有偶數(shù)的和 
  7. int count = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9).stream()//獲取順序流 
  8. .filter(x -> x % 2 == 0).// 2 4 6 8  
  9. mapToInt(x->x).sum();//求和 
  10. System.out.println(count); //打印輸出 20  

2. distinct:通過(guò)流中元素的 hashCode() 和 equals() 去除重復(fù)元素

  1. Arrays.asList(1,2,3,3,3,4,5,2).stream()//獲取順序流 
  2.  .distinct()//去重 
  3.  .forEach(System.out::println);// 打印輸出(1,2,3,4,5) 
  4.  
  5. System.out.println("去重:---------------"); 
  6.  
  7. Arrays.asList(1,2,3,3,3,4,5,2).stream()//獲取順序流 
  8.  .collect(Collectors.toSet())//Set()去重 
  9.  .forEach(System.out::println);// 打印輸出(1,2,3,4,5) 

3. 排序

sorted():返回由此流的元素組成的流,根據(jù)自然順序排序。sorted(Comparator com):返回由該流的元素組成的流,根據(jù)提供的 Comparator進(jìn)行排序。

  1. //獲取最大值和最小值但是不使用minmax方法 
  2.    List<Integer> list = Arrays.asList(1,2, 3,4, 5, 6); 
  3.    Optional<Integermin = list.stream().sorted().findFirst();//自然排序 根據(jù)數(shù)字從小到大排列 
  4.    System.out.println(min.get());//打印輸出(1) 
  5.     
  6.    Optional<Integer> max2 = list.stream().sorted((a, b) -> b - a).findFirst();//定時(shí)排序 根據(jù)最大數(shù)進(jìn)行排序 
  7.    System.out.println(max2.get());//打印輸出(6) 
  8.  
  9.  //按照大小(a-z)排序 
  10.  Arrays.asList("java","c#","python","scala").stream().sorted().forEach(System.out::println); 
  11.  //按照長(zhǎng)度排序 
  12.  Arrays.asList("java","c#","python","scala").stream().sorted((a,b)->a.length()-b.length()).forEach(System.out::println); 

4. 截取

limit(n):返回由此流的元素組成的流,截短長(zhǎng)度不能超過(guò) nskip(n):在丟棄流的第n元素后,配合limit(n)可實(shí)現(xiàn)分頁(yè)

  1. //打印20-30這樣的集合數(shù)據(jù) 
  2.       Stream.iterate(1,x->x+1).limit(50)// limit 50 總共到50 
  3.       .skip(20)// 跳過(guò)前 20 
  4.       .limit(10) // 打印10個(gè) 
  5.       .forEach(System.out::println);//打印輸出(21,22,23,24,25,26,27,28,29,30) 

5. 轉(zhuǎn)換

map:接收一個(gè)函數(shù)作為參數(shù),該函數(shù)會(huì)被應(yīng)用到每個(gè)元素上,并將其映射成一個(gè)新的元素。flatMap:接收一個(gè)函數(shù)作為參數(shù),將流中的每個(gè)值都換成另一個(gè)流,然后把所有流連接成一個(gè)流。

  1. List<String> list = Arrays.asList("a,b,c""1,2,3"); 
  2.   
  3. //將每個(gè)元素轉(zhuǎn)成一個(gè)新的且不帶逗號(hào)的元素 
  4. Stream<String> s1 = list.stream().map(s -> s.replaceAll(",""")); 
  5. s1.forEach(System.out::println); // abc  123 
  6.   
  7. Stream<String> s3 = list.stream().flatMap(s -> { 
  8.     //將每個(gè)元素轉(zhuǎn)換成一個(gè)stream 
  9.     String[] split = s.split(","); 
  10.     Stream<String> s2 = Arrays.stream(split); 
  11.     return s2; 
  12. }); 
  13. s3.forEach(System.out::println); // a b c 1 2 3 

6. 消費(fèi)

peek:如同于map,能得到流中的每一個(gè)元素。但map接收的是一個(gè)Function表達(dá)式,有返回值;而peek接收的是Consumer表達(dá)式,沒(méi)有返回值。

  1. //將str中的每一個(gè)數(shù)值都打印出來(lái),同時(shí)算出最終的求和結(jié)果 
  2. String str ="11,22,33,44,55";       
  3. System.out.println(Stream.of(str.split(",")).peek(System.out::println).mapToInt(Integer::valueOf).sum());//11 22 33 44 55 165 

5.2 終止操作

1. 循環(huán):forEach

Users類:

  1. import java.util.Date
  2.  
  3. /** 
  4.  * @program: lambda 
  5.  * @ClassName Users 
  6.  * @description: 
  7.  * @author: muxiaonong 
  8.  * @create: 2020-10-24 11:00 
  9.  * @Version 1.0 
  10.  **/ 
  11. public class Users { 
  12.  
  13.     private String name
  14.     public Users() {} 
  15.  
  16.     /** 
  17.      * @param name 
  18.      */ 
  19.     public Users(String name) { 
  20.         this.name = name
  21.     } 
  22.  
  23.     /** 
  24.      * @param name 
  25.      * @return 
  26.      */ 
  27.     public static Users build(String name){ 
  28.         Users u = new Users(); 
  29.         u.setName(name); 
  30.         return u; 
  31.     } 
  32.  
  33.     public String getName() { 
  34.         return name
  35.     } 
  36.  
  37.     public void setName(String name) { 
  38.         this.name = name
  39.     } 
  40.  
  41.     @Override 
  42.     public String toString() { 
  43.         return  "name='" + name + '\''
  44.     } 
  45.   } 
  1. //創(chuàng)建一組自定義對(duì)象 
  2. String str2 = "java,scala,python"
  3. Stream.of(str2.split(",")).map(x->new Users(x)).forEach(System.out::println);//打印輸出(name='java' name='scala' name='python') 
  4. Stream.of(str2.split(",")).map(Users::new).forEach(System.out::println);//打印輸出(name='java' name='scala' name='python') 
  5. Stream.of(str2.split(",")).map(x->Users.build(x)).forEach(System.out::println);//打印輸出(name='java' name='scala' name='python') 
  6. Stream.of(str2.split(",")).map(Users::build).forEach(System.out::println);//打印輸出(name='java' name='scala' name='python') 

2. 計(jì)算:min、max、count、sum

min:返回流中元素最小值max:返回流中元素最大值count:返回流中元素的總個(gè)數(shù)sum:求和

  1. //求集合中的最大值 
  2. List<Integer> list = Arrays.asList(1,2, 3,4, 5, 6); 
  3.  Optional<Integermax = list.stream().max((a, b) -> a - b); 
  4.  System.out.println(max.get()); // 6  
  5.  //求集合的最小值 
  6.  System.out.println(list.stream().min((a, b) -> a-b).get()); // 1 
  7. //求集合的總個(gè)數(shù) 
  8. System.out.println(list.stream().count());//6 
  9.  //求和 
  10.  String str ="11,22,33,44,55"
  11.  System.out.println(Stream.of(str.split(",")).mapToInt(x -> Integer.valueOf(x)).sum()); 
  12.  System.out.println(Stream.of(str.split(",")).mapToInt(Integer::valueOf).sum()); 
  13.  System.out.println(Stream.of(str.split(",")).map(x -> Integer.valueOf(x)).mapToInt(x -> x).sum()); 
  14.  System.out.println(Stream.of(str.split(",")).map(Integer::valueOf).mapToInt(x -> x).sum()); 

3. 匹配:anyMatch、 allMatch、 noneMatch、 findFirst、 findAny

anyMatch:接收一個(gè) Predicate 函數(shù),只要流中有一個(gè)元素滿足該斷言則返回true,否則返回falseallMatch:接收一個(gè) Predicate 函數(shù),當(dāng)流中每個(gè)元素都符合該斷言時(shí)才返回true,否則返回falsenoneMatch:接收一個(gè) Predicate 函數(shù),當(dāng)流中每個(gè)元素都不符合該斷言時(shí)才返回true,否則返回falsefindFirst:返回流中第一個(gè)元素findAny:返回流中的任意元素

  1. List<Integer> list = Arrays.asList(1,2, 3,4, 5, 6); 
  2. System.out.println(list.stream().allMatch(x -> x>=0)); //如果集合中的元素大于等于0 返回true 
  3. System.out.println(list.stream().noneMatch(x -> x > 5));//如果集合中的元素有大于5的元素。返回false 
  4. System.out.println(list.stream().anyMatch(x -> x > 4));//如果集合中有大于四4的元素,返回true 
  5. //取第一個(gè)偶數(shù) 
  6. Optional<Integerfirst = list.stream().filter(x -> x % 10 == 6).findFirst(); 
  7. System.out.println(first.get());// 6 
  8. //任意取一個(gè)偶數(shù) 
  9. Optional<Integerany = list.stream().filter(x -> x % 2 == 0).findAny(); 
  10. System.out.println(any.get());// 2 

4.收集器:toArray、collect

collect:接收一個(gè)Collector實(shí)例,將流中元素收集成另外一個(gè)數(shù)據(jù)結(jié)構(gòu)Collector

  1. Supplier supplier();創(chuàng)建一個(gè)結(jié)果容器A
  2. BiConsumer
  3. BinaryOperator combiner();函數(shù)接口,該參數(shù)的作用跟上一個(gè)方法(reduce)中的combiner參數(shù)一樣,將并行流中各個(gè)子進(jìn)程的運(yùn)行結(jié)果(accumulator函數(shù)操作后的容器A)進(jìn)行合并。
  4. Function
  5. Set characteristics();返回一個(gè)不可變的Set集合,用來(lái)表明該Collector的特征
  1. /** 
  2.  * @program: lambda 
  3.  * @ClassName Customer 
  4.  * @description: 
  5.  * @author: muxiaonong 
  6.  * @create: 2020-10-24 11:36 
  7.  * @Version 1.0 
  8.  **/ 
  9. public class Customer { 
  10.  
  11.     private String name
  12.  
  13.     private Integer age; 
  14.      
  15. ...getset忽略 
  16.  public static void main(String[] args) { 
  17.         Customer c1 = new Customer("張三",10); 
  18.         Customer c2 = new Customer("李四",20); 
  19.         Customer c3 = new Customer("王五",10); 
  20.  
  21.         List<Customer> list = Arrays.asList(c1,c2,c3); 
  22.  
  23.         //轉(zhuǎn)成list 
  24.         List<Integer> ageList = list.stream().map(Customer::getAge).collect(Collectors.toList()); 
  25.         System.out.println("ageList:"+ageList);//ageList:[10, 20, 10] 
  26.  
  27.         //轉(zhuǎn)成set 
  28.         Set<Integer> ageSet = list.stream().map(Customer::getAge).collect(Collectors.toSet()); 
  29.         System.out.println("ageSet:"+ageSet);//ageSet:[20, 10] 
  30.  
  31. //轉(zhuǎn)成map,注:key不能相同,否則報(bào)錯(cuò) 
  32.         Map<String, Integer> CustomerMap = list.stream().collect(Collectors.toMap(Customer::getName, Customer::getAge)); 
  33.         System.out.println("CustomerMap:"+CustomerMap);//CustomerMap:{李四=20, 張三=10, 王五=10} 
  34.  
  35. //字符串分隔符連接 
  36.         String joinName = list.stream().map(Customer::getName).collect(Collectors.joining(",""("")")); 
  37.         System.out.println("joinName:"+joinName);//joinName:(張三,李四,王五) 
  38.  
  39. //聚合操作 
  40. //1.學(xué)生總數(shù) 
  41.         Long count = list.stream().collect(Collectors.counting()); 
  42.         System.out.println("count:"+count);//count:3 
  43. //2.最大年齡 (最小的minBy同理) 
  44.         Integer maxAge = list.stream().map(Customer::getAge).collect(Collectors.maxBy(Integer::compare)).get(); 
  45.         System.out.println("maxAge:"+maxAge);//maxAge:20 
  46.  
  47. //3.所有人的年齡 
  48.         Integer sumAge = list.stream().collect(Collectors.summingInt(Customer::getAge)); 
  49.         System.out.println("sumAge:"+sumAge);//sumAge:40 
  50.  
  51. //4.平均年齡 
  52.         Double averageAge = list.stream().collect(Collectors.averagingDouble(Customer::getAge)); 
  53.         System.out.println("averageAge:"+averageAge);//averageAge:13.333333333333334 
  54.  
  55. //分組 
  56.         Map<Integer, List<Customer>> ageMap = list.stream().collect(Collectors.groupingBy(Customer::getAge)); 
  57.         System.out.println("ageMap:"+ageMap);//ageMap:{20=[com.mashibing.stream.Customer@20ad9418], 10=[com.mashibing.stream.Customer@31cefde0, com.mashibing.stream.Customer@439f5b3d]} 
  58.  
  59.  
  60. //分區(qū) 
  61. //分成兩部分,一部分大于10歲,一部分小于等于10歲 
  62.         Map<Boolean, List<Customer>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10)); 
  63.         System.out.println("partMap:"+partMap); 
  64.  
  65. //規(guī)約 
  66.         Integer allAge = list.stream().map(Customer::getAge).collect(Collectors.reducing(Integer::sum)).get(); 
  67.         System.out.println("allAge:"+allAge);//allAge:40 
  68.  
  69.  
  70.     } 
  1.  public static void main(String[] args) { 
  2.         Customer c1 = new Customer("張三",10); 
  3.         Customer c2 = new Customer("李四",20); 
  4.         Customer c3 = new Customer("王五",10); 
  5.  
  6.         List<Customer> list = Arrays.asList(c1,c2,c3); 
  7.  
  8.         //轉(zhuǎn)成list 
  9.         List<Integer> ageList = list.stream().map(Customer::getAge).collect(Collectors.toList()); 
  10.         System.out.println("ageList:"+ageList);//ageList:[10, 20, 10] 
  11.  
  12.         //轉(zhuǎn)成set 
  13.         Set<Integer> ageSet = list.stream().map(Customer::getAge).collect(Collectors.toSet()); 
  14.         System.out.println("ageSet:"+ageSet);//ageSet:[20, 10] 
  15.  
  16. //轉(zhuǎn)成map,注:key不能相同,否則報(bào)錯(cuò) 
  17.         Map<String, Integer> CustomerMap = list.stream().collect(Collectors.toMap(Customer::getName, Customer::getAge)); 
  18.         System.out.println("CustomerMap:"+CustomerMap);//CustomerMap:{李四=20, 張三=10, 王五=10} 
  19.  
  20. //字符串分隔符連接 
  21.         String joinName = list.stream().map(Customer::getName).collect(Collectors.joining(",""("")")); 
  22.         System.out.println("joinName:"+joinName);//joinName:(張三,李四,王五) 
  23.  
  24. //聚合操作 
  25. //1.學(xué)生總數(shù) 
  26.         Long count = list.stream().collect(Collectors.counting()); 
  27.         System.out.println("count:"+count);//count:3 
  28. //2.最大年齡 (最小的minBy同理) 
  29.         Integer maxAge = list.stream().map(Customer::getAge).collect(Collectors.maxBy(Integer::compare)).get(); 
  30.         System.out.println("maxAge:"+maxAge);//maxAge:20 
  31.  
  32. //3.所有人的年齡 
  33.         Integer sumAge = list.stream().collect(Collectors.summingInt(Customer::getAge)); 
  34.         System.out.println("sumAge:"+sumAge);//sumAge:40 
  35.  
  36. //4.平均年齡 
  37.         Double averageAge = list.stream().collect(Collectors.averagingDouble(Customer::getAge)); 
  38.         System.out.println("averageAge:"+averageAge);//averageAge:13.333333333333334 
  39.  
  40. //分組 
  41.         Map<Integer, List<Customer>> ageMap = list.stream().collect(Collectors.groupingBy(Customer::getAge)); 
  42.         System.out.println("ageMap:"+ageMap);//ageMap:{20=[com.mashibing.stream.Customer@20ad9418], 10=[com.mashibing.stream.Customer@31cefde0, com.mashibing.stream.Customer@439f5b3d]} 
  43.  
  44.  
  45. //分區(qū) 
  46. //分成兩部分,一部分大于10歲,一部分小于等于10歲 
  47.         Map<Boolean, List<Customer>> partMap = list.stream().collect(Collectors.partitioningBy(v -> v.getAge() > 10)); 
  48.         System.out.println("partMap:"+partMap); 
  49.  
  50. //規(guī)約 
  51.         Integer allAge = list.stream().map(Customer::getAge).collect(Collectors.reducing(Integer::sum)).get(); 
  52.         System.out.println("allAge:"+allAge);//allAge:40 
  53.  
  54.  
  55.     } 

六、Stream的方法摘要

 

修飾符和類型 方法和說(shuō)明
staticCollector<T,?,Double> averagingDouble(ToDoubleFunction<? super T> mapper) 返回一個(gè) Collector ,它產(chǎn)生應(yīng)用于輸入元素的雙值函數(shù)的算術(shù)平均值。
staticCollector<T,?,Double> averagingInt(ToIntFunction<? super T> mapper) 返回一個(gè) Collector ,它產(chǎn)生應(yīng)用于輸入元素的整數(shù)值函數(shù)的算術(shù)平均值。
staticCollector<T,?,Double> averagingLong(ToLongFunction<? super T> mapper) 返回一個(gè) Collector ,它產(chǎn)生應(yīng)用于輸入元素的長(zhǎng)值函數(shù)的算術(shù)平均值。
static <T,A,R,RR> Collector<T,A,RR> collectingAndThen(Collector<T,A,R> downstream, Function<R,RR> finisher) 適應(yīng) Collector進(jìn)行額外的整理轉(zhuǎn)換。
staticCollector<T,?,Long> counting() 返回 Collector類型的接受元件 T計(jì)數(shù)輸入元件的數(shù)量。
static <T,K> Collector<T,?,Map<K,List>> groupingBy(Function<? super T,? extends K> classifier) 返回 Collector “由基團(tuán)”上的類型的輸入元件操作實(shí)現(xiàn) T ,根據(jù)分類功能分組元素,并且在返回的結(jié)果 Map 。
static <T,K,A,D> Collector<T,?,Map<K,D>> groupingBy(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream) 返回 Collector “由基團(tuán)”上的類型的輸入元件操作實(shí)現(xiàn)級(jí)聯(lián) T ,根據(jù)分類功能分組元素,然后使用下游的指定執(zhí)行與給定鍵相關(guān)聯(lián)的值的歸約運(yùn)算 Collector 。
static <T,K,D,A,M extends Map<K,D>>Collector<T,?,M> groupingBy(Function<? super T,? extends K> classifier, SuppliermapFactory, Collector<? super T,A,D> downstream) 返回 Collector “由基團(tuán)”上的類型的輸入元件操作實(shí)現(xiàn)級(jí)聯(lián) T ,根據(jù)分類功能分組元素,然后使用下游的指定執(zhí)行與給定鍵相關(guān)聯(lián)的值的歸約運(yùn)算 Collector 。
static <T,K> Collector<T,?,ConcurrentMap<K,List>> groupingByConcurrent(Function<? super T,? extends K> classifier) 返回一個(gè)并發(fā) Collector “由基團(tuán)”上的類型的輸入元件操作實(shí)現(xiàn) T ,根據(jù)分類功能分組元素。
static <T,K,A,D> Collector<T,?,ConcurrentMap<K,D>> groupingByConcurrent(Function<? super T,? extends K> classifier, Collector<? super T,A,D> downstream) 返回一個(gè)并發(fā) Collector “由基團(tuán)”上的類型的輸入元件操作實(shí)現(xiàn)級(jí)聯(lián) T ,根據(jù)分類功能分組元素,然后使用下游的指定執(zhí)行與給定鍵相關(guān)聯(lián)的值的歸約運(yùn)算 Collector 。
static <T,K,A,D,M extends ConcurrentMap<K,D>> Collector<T,?,M> groupingByConcurrent(Function<? super T,? extends K> classifier, SuppliermapFactory, Collector<? super T,A,D> downstream) 返回一個(gè)并發(fā) Collector “由基團(tuán)”上的類型的輸入元件操作實(shí)現(xiàn)級(jí)聯(lián) T ,根據(jù)分類功能分組元素,然后使用下游的指定執(zhí)行與給定鍵相關(guān)聯(lián)的值的歸約運(yùn)算 Collector 。
static Collector<CharSequence,?,String> joining() 返回一個(gè) Collector ,按照遇到的順序?qū)⑤斎朐剡B接到一個(gè) String中。
static Collector<CharSequence,?,String> joining(CharSequence delimiter) 返回一個(gè) Collector ,按照遇到的順序連接由指定的分隔符分隔的輸入元素。
static Collector<CharSequence,?,String> joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix) 返回一個(gè) Collector ,它將按照指定的 Collector分隔的輸入元素與指定的前綴和后綴進(jìn)行連接。
static <T,U,A,R> Collector<T,?,R> mapping(Function<? super T,? extends U> mapper, Collector<? super U,A,R> downstream) 適應(yīng)一個(gè) Collector類型的接受元件 U至類型的一個(gè)接受元件 T通過(guò)積累前應(yīng)用映射函數(shù)到每個(gè)輸入元素。
staticCollector<T,?,Optional> maxBy(Comparator<? super T> comparator) 返回一個(gè) Collector ,它根據(jù)給出的 Comparator產(chǎn)生最大元素,描述為 Optional。
staticCollector<T,?,Optional> minBy(Comparator<? super T> comparator) 返回一個(gè) Collector ,根據(jù)給出的 Comparator產(chǎn)生最小元素,描述為 Optional。
staticCollector<T,?,Map<Boolean,List>> partitioningBy(Predicate<? super T> predicate) 返回一個(gè) Collector ,根據(jù)Predicate對(duì)輸入元素進(jìn)行 Predicate ,并將它們組織成 Map<Boolean, List> 。
static <T,D,A> Collector<T,?,Map<Boolean,D>> partitioningBy(Predicate<? super T> predicate, Collector<? super T,A,D> downstream) 返回一個(gè) Collector ,它根據(jù)Predicate對(duì)輸入元素進(jìn)行 Predicate ,根據(jù)另一個(gè) Collector減少每個(gè)分區(qū)的值,并將其組織成 Map<Boolean, D> ,其值是下游縮減的結(jié)果。
staticCollector<T,?,Optional> reducing(BinaryOperatorop) 返回一個(gè) Collector ,它在指定的 Collector下執(zhí)行其輸入元素的 BinaryOperator 。
staticCollector<T,?,T> reducing(T identity, BinaryOperatorop) 返回 Collector執(zhí)行下一個(gè)指定的減少其輸入元件的 BinaryOperator使用所提供的身份。
static <T,U> Collector<T,?,U> reducing(U identity, Function<? super T,? extends U> mapper, BinaryOperator op) 返回一個(gè) Collector ,它在指定的映射函數(shù)和 BinaryOperator下執(zhí)行其輸入元素的 BinaryOperator 。
staticCollector<T,?,DoubleSummaryStatistics> summarizingDouble(ToDoubleFunction<? super T> mapper) 返回一個(gè) Collector , double生產(chǎn)映射函數(shù)應(yīng)用于每個(gè)輸入元素,并返回結(jié)果值的匯總統(tǒng)計(jì)信息。
staticCollector<T,?,IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) 返回一個(gè) Collector , int生產(chǎn)映射函數(shù)應(yīng)用于每個(gè)輸入元素,并返回結(jié)果值的匯總統(tǒng)計(jì)信息。
staticCollector<T,?,LongSummaryStatistics> summarizingLong(ToLongFunction<? super T> mapper) 返回一個(gè) Collector , long生產(chǎn)映射函數(shù)應(yīng)用于每個(gè)輸入元素,并返回結(jié)果值的匯總統(tǒng)計(jì)信息。
staticCollector<T,?,Double> summingDouble(ToDoubleFunction<? super T> mapper) 返回一個(gè) Collector ,它產(chǎn)生應(yīng)用于輸入元素的雙值函數(shù)的和。
staticCollector<T,?,Integer> summingInt(ToIntFunction<? super T> mapper) 返回一個(gè) Collector ,它產(chǎn)生應(yīng)用于輸入元素的整數(shù)值函數(shù)的和。
staticCollector<T,?,Long> summingLong(ToLongFunction<? super T> mapper) 返回一個(gè) Collector ,它產(chǎn)生應(yīng)用于輸入元素的長(zhǎng)值函數(shù)的和。
static <T,C extends Collection> Collector<T,?,C> toCollection(SuppliercollectionFactory) 返回一個(gè) Collector ,按照遇到的順序?qū)⑤斎朐乩奂拥揭粋€(gè)新的 Collection中。
static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper) 返回一個(gè)并發(fā)的 Collector ,它將元素累加到 ConcurrentMap ,其鍵和值是將所提供的映射函數(shù)應(yīng)用于輸入元素的結(jié)果。
static <T,K,U> Collector<T,?,ConcurrentMap<K,U>> toConcurrentMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator mergeFunction) 返回一個(gè)并發(fā)的 Collector ,它將元素累加到一個(gè) ConcurrentMap ,其鍵和值是將提供的映射函數(shù)應(yīng)用于輸入元素的結(jié)果。
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 mergeFunction, SuppliermapSupplier) 返回一個(gè)并發(fā)的 Collector ,它將元素累加到一個(gè) ConcurrentMap ,其鍵和值是將所提供的映射函數(shù)應(yīng)用于輸入元素的結(jié)果。
staticCollector<T,?,List> toList() 返回一個(gè) Collector ,它將輸入元素 List到一個(gè)新的 List 。
static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper) 返回一個(gè) Collector ,它將元素累加到一個(gè) Map ,其鍵和值是將所提供的映射函數(shù)應(yīng)用于輸入元素的結(jié)果。
static <T,K,U> Collector<T,?,Map<K,U>> toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper, BinaryOperator mergeFunction)  返回一個(gè) Collector ,它將元素累加到 Map ,其鍵和值是將提供的映射函數(shù)應(yīng)用于輸入元素的結(jié)果。
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 mergeFunction, SuppliermapSupplier) 返回一個(gè) Collector ,它將元素累加到一個(gè) Map ,其鍵和值是將所提供的映射函數(shù)應(yīng)用于輸入元素的結(jié)果。
staticCollector<T,?,Set> toSet() 返回一個(gè) Collector ,將輸入元素 Set到一個(gè)新的 Set 。

七、總結(jié)

 

對(duì)于Java中新特性除了 Stream 還有l(wèi)amaba表達(dá)式都是可以幫忙我們很好的去優(yōu)化代碼,使我們的代碼簡(jiǎn)潔且意圖明確,避免繁瑣的重復(fù)性的操作,對(duì)于文中有興趣的小伙伴可以操作起來(lái),又不懂的小伙伴可以在下面進(jìn)行留言,小農(nóng)看到了會(huì)第一時(shí)間回復(fù)大家,謝謝,大家加油!

原文地址:https://mp.weixin.qq.com/s/LuV5QGSfP60EPWBTeY3SuQ

延伸 · 閱讀

精彩推薦
464
主站蜘蛛池模板: 1000部精品久久久久久久久 | 国产一区二区三区成人 | 国产最好的精华液网站 | 欧美精品三区 | 国产一区二区三区在线免费观看 | 国产亚洲欧美美 | 性做久久久久久 | 毛片免费观看视频 | 亚洲国产精品一区二区第一页 | 国产一区二区在线免费观看 | 性高潮一级片 | 欧美成人二区 | 久久久久久久久久久久久久免费看 | 日韩欧美一区二区在线观看视频 | 欧美日韩国产在线观看 | 午夜四虎| 亚洲综合区 | 午夜欧美一区二区三区在线播放 | 涩涩视频在线免费看 | 久久精品一区二区三区四区 | www.av在线.com| 亚洲精品一二区 | 欧美日韩综合精品 | 精品伦精品一区二区三区视频 | 国产小视频在线 | 999精品在线 | 国产免费一区二区三区 | 香蕉久久夜色精品国产使用方法 | 久久精品日韩 | 成人性生交大片免费看网站 | 91精品久久久久久久久久 | 日韩三级电影 | 午夜精品久久久久 | 亚洲一区二区三区视频 | 私人毛片免费高清视频 | 亚洲成人一区二区三区 | 午夜黄色影院 | 亚洲综合视频在线 | 在线视频一区二区 | 中文在线一区二区 | 九九热免费观看 |