java规约运算的signature_Java8笔记第七篇(Stream API 的操作-规约,收集 )
深入 java8 第 07 篇 ( Stream API 的操作->規(guī)約,收集 )
一、Stream API 的操作步驟:
創(chuàng)建 Stream
中間操作
終止操作(終端操作 -> 規(guī)約,收集)
7.1 規(guī)約
reduce(T iden, BinaryOperator b) 可以將流中元素反復(fù)結(jié)合起來,得到一個(gè)值。 返回 T
reduce(BinaryOperator b) 可以將流中元素反復(fù)結(jié)合起來,得到一個(gè)值。 返回 Optional
7.1.1 準(zhǔn)備測(cè)試數(shù)據(jù)
List emps = Arrays.asList(
new Employee(001, "Emma", 41, 20000, Status.FREE),
new Employee(002, "Mary", 39, 18000, Status.BUSY),
new Employee(003, "Allen", 33, 15000, Status.BUSY),
new Employee(004, "Olivia", 52, 32000, Status.FREE),
new Employee(005, "Natasha", 27, 13000, Status.BUSY),
new Employee(006, "Kevin", 25, 10000, Status.FREE),
new Employee(007, "Haivent", 25, 12000, Status.FREE)
);
List numList = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
7.1.2 測(cè)試規(guī)約
@Test
public void test01(){
// 所有整數(shù)累加和
Integer reduce = numList.stream()
.reduce(0, (x,y)->x+y); System.out.println(reduce); // 所有薪資累加和 Optional reduce2 = emps.stream() .map(Employee::getSalary) .reduce(Double::sum); System.out.println("\n"+reduce2.get()); }
7.1.3 需求:統(tǒng)計(jì)所有姓名中 “A” 的出現(xiàn)次數(shù)
@Test
public void test02(){
Optional reduce = emps.stream()
.map(Employee::getName)
.flatMap(StreamAPI03::filterCharacter)
.map((e)->{ if(e.equals('A')){ return 1; }else{ return 0; } }) .reduce(Integer::sum); System.out.println(reduce.get()); } private static Stream filterCharacter(String str){ List list = new ArrayList<>(); for (Character ch : str.toCharArray()) { list.add(ch); } return list.stream(); }
7.2 收集
collect(Collector c) 將流轉(zhuǎn)換為其他形式。接收一個(gè) Collector接口的實(shí)現(xiàn),用于給Stream中元素做匯總的方法。
Collector 接口中方法的實(shí)現(xiàn)決定了如何對(duì)流執(zhí)行收集操作(如收集到 List、 Set、 Map)。但是 Collectors 實(shí)用類提供了很多靜態(tài)方法,可以方便地創(chuàng)建常見收集器實(shí)例, 具體方法與實(shí)例如下:
方法與實(shí)例:
toList
List 把流中元素收集到List
toSet
Set 把流中元素收集到Set
toCollection
Collection 把流中元素收集到創(chuàng)建的集合
counting
Long 計(jì)算流中元素的個(gè)數(shù)
summingInt
Integer 對(duì)流中元素的整數(shù)屬性求和
averagingInt
Double 計(jì)算流中元素Integer屬性的平均值
summarizingInt
IntSummaryStatistics 收集流中Integer屬性的統(tǒng)計(jì)值。如:平均值
joining
String 連接流中每個(gè)字符串
maxBy
Optional 根據(jù)比較器選擇最大值
minBy
Optional 根據(jù)比較器選擇最小值
reducing
歸約產(chǎn)生的類型 從一個(gè)作為累加器的初始值開始,利用BinaryOperator與流中元素逐個(gè)結(jié)合,從而歸約成單個(gè)值
collectingAndThen
轉(zhuǎn)換函數(shù)返回的類型 包裹另一個(gè)收集器,對(duì)其結(jié)果轉(zhuǎn)換函數(shù)
groupingBy
Map> 根據(jù)某屬性值對(duì)流分組,屬性為K,結(jié)果為V
partitioningBy
Map> 根據(jù)true或false進(jìn)行分區(qū)
7.2.1 測(cè)試收集 收集到集合
@Test
public void test03(){
List collect = emps.stream()
.map(Employee::getName)
.collect(Collectors.toList());
collect.forEach(System.out::println);
System.out.println();
Set collect2 = emps.stream()
.map(Employee::getName)
.collect(Collectors.toSet());
collect2.forEach(System.out::println);
System.out.println();
ArrayList collect3 = emps.stream()
.map(Employee::getName)
.collect(Collectors.toCollection(ArrayList::new));
collect3.forEach(System.out::println);
}
7.2.3 對(duì)流中元素進(jìn)行計(jì)算
@Test
public void test04(){
Long collect = emps.stream()
.collect(Collectors.counting());
System.out.println("總員工數(shù):"+collect);
Double collect2 = emps.stream()
.collect(Collectors.summingDouble(Employee::getSalary));
System.out.println("\n每月應(yīng)發(fā)薪資總額:"+collect2);
Double collect3 = emps.stream()
.collect(Collectors.averagingDouble(Employee::getSalary));
System.out.println("\n員工平均月薪:"+collect3);
DoubleSummaryStatistics collect4 = emps.stream()
.collect(Collectors.summarizingDouble(Employee::getSalary));
System.out.println("\n員工中最低薪資為:"+collect4.getMin());
Optional collect5 = emps.stream()
.map(Employee::getSalary)
.collect(Collectors.maxBy(Double::compare));
System.out.println("\n員工中薪資最高為:"+collect5.get());
Optional collect6 = emps.stream()
.map(Employee::getSalary)
.collect(Collectors.minBy(Double::compare));
System.out.println("\n員工中薪資最低為:"+collect6.get());
Optional collect7 = emps.stream()
.map(Employee::getSalary)
.collect(Collectors.reducing(Double::sum));
System.out.println("\n員工每月薪資總共為:"+collect7.get());
}
7.2.4 將多個(gè)字符串連接成一個(gè)字符串
@Test
public void test05(){
String collect = emps.stream()
.map(Employee::getName)
.collect(Collectors.joining("-"));
System.out.println(collect);
}
7.2.5 對(duì)類型轉(zhuǎn)換為集合后,返回集合大小
@Test
public void test06(){
Integer collect = emps.stream()
.collect(Collectors.collectingAndThen(Collectors.toList(), List::size));
System.out.println(collect);
}
7.2.6 單級(jí)分組
@Test
public void test07(){
Map> collect = emps.stream()
.collect(Collectors.groupingBy(Employee::getStatus));
System.out.println(collect);
}
7.2.7 多級(jí)分組
@Test
public void test08(){
Map>> collect = emps.stream()
.collect(Collectors.groupingBy((e)->{ if(e.getStatus() == Status.FREE){ return "空閑"; }else{ return "繁忙"; } }, Collectors.groupingBy((e)->{ if(e.getSalary() > 10000){ return "大于 15000"; }else if (e.getSalary() > 20000){ return "大于 20000"; }else{ return "大于25000"; } }))); System.out.println(collect); }
根據(jù)員工狀態(tài)進(jìn)行分區(qū)
@Test
public void test09(){
Map> collect = emps.stream()
.collect(Collectors.partitioningBy((e)->e.getStatus() == Status.FREE)); System.out.println(collect); }
總結(jié)
以上是生活随笔為你收集整理的java规约运算的signature_Java8笔记第七篇(Stream API 的操作-规约,收集 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python·代码高亮插件YAPF
- 下一篇: php mysql环境 xp_MySQL