一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - JDK新特性——Stream代碼簡潔之道

JDK新特性——Stream代碼簡潔之道

2021-05-06 23:02牧小農 Java教程

Stream 是 Java8 中處理集合的關鍵抽象概念,它可以指定你希望對集合進行的操作,可以執行非常復雜的查找、過濾和映射數據等操作.

JDK新特性——Stream代碼簡潔之道

一、概述

 

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

代碼簡潔函數式編程寫出的代碼簡潔且意圖明確,使用stream接口讓你從此告別for循環。

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

Stream 是 Java8 中處理集合的關鍵抽象概念,它可以指定你希望對集合進行的操作,可以執行非常復雜的查找、過濾和映射數據等操作

二、Stream特性

 

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

三、分類

 

關于應用在Stream流上的操作,可以分成兩種:

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

使用Stream流,可以清楚地知道我們要對一個數據集做何種操作,可讀性強。而且可以很輕松地獲取并行化Stream流,不用自己編寫多線程代碼,可以讓我們更加專注于業務邏輯。

JDK新特性——Stream代碼簡潔之道

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

四、Stream的創建

 

1、通過數組來生成 2、通過集合來生成 3、通過Stream.generate方法來創建 4、通過Stream.iterate方法來創建 5、其他Api創建

4.1 通過數組來生成

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

4.2 通過集合來生成

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

4.3 通過Stream.generate方法來創建

  1. //generate 
  2. static void gen3(){ 
  3.     Stream<Integer> generate = Stream.generate(() -> 1);//使用Stream中的靜態方法:generate() 
  4.     //limit 返回由該流的元素組成的流,截斷長度不能超過maxSize 
  5.     generate.limit(10).forEach(System.out::println);//打印輸出(打印10個1) 

4.4 通過Stream.iterate方法來創建

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

4.5其他Api創建

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

五、Stream的常用API

 

5.1 中間操作

1. filter:過濾流中的某些元素

  1. //中間操作:如果調用方法之后返回的結果是Stream對象就意味著是一個中間操作 
  2.  Arrays.asList(1,2,3,4,5).stream()//獲取順序流 
  3.  .filter((x)->x%2==0) // 2 4  
  4.  .forEach(System.out::println); 
  5.  
  6. //求出結果集中所有偶數的和 
  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:通過流中元素的 hashCode() 和 equals() 去除重復元素

  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():返回由此流的元素組成的流,根據自然順序排序。sorted(Comparator com):返回由該流的元素組成的流,根據提供的 Comparator進行排序。

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

4. 截取

limit(n):返回由此流的元素組成的流,截短長度不能超過 nskip(n):在丟棄流的第n元素后,配合limit(n)可實現分頁

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

5. 轉換

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

  1. List<String> list = Arrays.asList("a,b,c""1,2,3"); 
  2.   
  3. //將每個元素轉成一個新的且不帶逗號的元素 
  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.     //將每個元素轉換成一個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. 消費

peek:如同于map,能得到流中的每一個元素。但map接收的是一個Function表達式,有返回值;而peek接收的是Consumer表達式,沒有返回值。

  1. //將str中的每一個數值都打印出來,同時算出最終的求和結果 
  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. 循環: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. //創建一組自定義對象 
  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. 計算:min、max、count、sum

min:返回流中元素最小值max:返回流中元素最大值count:返回流中元素的總個數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. //求集合的總個數 
  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:接收一個 Predicate 函數,只要流中有一個元素滿足該斷言則返回true,否則返回falseallMatch:接收一個 Predicate 函數,當流中每個元素都符合該斷言時才返回true,否則返回falsenoneMatch:接收一個 Predicate 函數,當流中每個元素都不符合該斷言時才返回true,否則返回falsefindFirst:返回流中第一個元素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. //取第一個偶數 
  6. Optional<Integerfirst = list.stream().filter(x -> x % 10 == 6).findFirst(); 
  7. System.out.println(first.get());// 6 
  8. //任意取一個偶數 
  9. Optional<Integerany = list.stream().filter(x -> x % 2 == 0).findAny(); 
  10. System.out.println(any.get());// 2 

4.收集器:toArray、collect

collect:接收一個Collector實例,將流中元素收集成另外一個數據結構Collector

  1. Supplier supplier();創建一個結果容器A
  2. BiConsumer
  3. BinaryOperator combiner();函數接口,該參數的作用跟上一個方法(reduce)中的combiner參數一樣,將并行流中各個子進程的運行結果(accumulator函數操作后的容器A)進行合并。
  4. Function
  5. Set characteristics();返回一個不可變的Set集合,用來表明該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.         //轉成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.         //轉成set 
  28.         Set<Integer> ageSet = list.stream().map(Customer::getAge).collect(Collectors.toSet()); 
  29.         System.out.println("ageSet:"+ageSet);//ageSet:[20, 10] 
  30.  
  31. //轉成map,注:key不能相同,否則報錯 
  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.學生總數 
  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. //分區 
  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. //規約 
  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.         //轉成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.         //轉成set 
  13.         Set<Integer> ageSet = list.stream().map(Customer::getAge).collect(Collectors.toSet()); 
  14.         System.out.println("ageSet:"+ageSet);//ageSet:[20, 10] 
  15.  
  16. //轉成map,注:key不能相同,否則報錯 
  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.學生總數 
  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. //分區 
  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. //規約 
  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的方法摘要

 

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

七、總結

 

對于Java中新特性除了 Stream 還有lamaba表達式都是可以幫忙我們很好的去優化代碼,使我們的代碼簡潔且意圖明確,避免繁瑣的重復性的操作,對于文中有興趣的小伙伴可以操作起來,又不懂的小伙伴可以在下面進行留言,小農看到了會第一時間回復大家,謝謝,大家加油!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产精品久久无套麻豆 | 亚洲香蕉伊在人在线观看9 亚洲系列国产系列 | 私人影院免费观看 | 俄罗斯处女 | segou视频在线观看 | 国产在线步兵一区二区三区 | 久久视频精品3线视频在线观看 | 欧美国产在线观看 | 关晓彤被调教出奶水的视频 | 国产一页| 欧美贵妇videos办公室360 | 免费日韩 | 喜欢老头吃我奶躁我的动图 | 精品一区二区高清在线观看 | 欧美办公室激情videos高清 | 91午夜剧场| 美女被扒开屁股进去网 | 侵犯小男生免费视频网站 | 日本深夜视频 | 国产精品理论片 | 亚洲www在线 | 欧美成人精品第一区二区三区 | 青草国产在线视频 | 男人女人日批 | 国产成人精视频在线观看免费 | 国产激情久久久久影院小草 | 亚洲高清国产拍精品影院 | 校园春色偷拍自拍 | 精品免费视频 | 污小说在线阅读 | 四虎影院免费在线播放 | 激情三级做爰在线观看激情 | 91嫩草国产在线观看免费 | 亚洲欧美国产在线 | 超级乱淫伦小说全集np | 91精品综合久久久久久五月天 | 久久 这里只精品 免费 | 国产精品久久久久久久久 | 亚欧洲乱码视频一二三区 | 女人肮脏的交易中文字幕未删减版 | 国产免费视 |