jdk1.8新特性的应用-Stream 的终止操作
public class TestStreamApi4 {
List<Employee> emps = Arrays.asList(
new Employee("张三", 23, 14000.11, "深圳"),
new Employee("李四", 34, 26000.33, "北京"),
new Employee("王五", 55, 18000.00, "上海"),
new Employee("赵六", 32, 22000.88, "广州"),
new Employee("田七", 23, 14000.11, "深圳")
);
/*
* 收集
* collect--将流转换为其他形式.接收一个Collector接口得实现,用于给Stream中的元素做汇总的方法
* */
@Test
public void test1() {
//1.收集所有员工的姓名
List<String> list = emps.stream().map(Employee::getName).collect(Collectors.toList());
System.out.println("list = " + list);
//list = [张三, 李四, 王五, 赵六, 田七]
}
@Test
public void test2() {
//2.收集所有员工的地址并去重 Employee::getAddress = ->e.getAddress()
Set<String> set = emps.stream().map(Employee::getAddress).collect(Collectors.toSet());
System.out.println("set = " + set);
//set = [上海, 广州, 深圳, 北京]
}
/*
* 计算
* */
@Test
public void test3() {
//总数
Long lo = emps.stream().collect(Collectors.counting());
System.out.println("lo = " + lo); //lo = 5
//平均值
Double aDouble = emps.stream().collect(Collectors.averagingDouble(Employee::getSalary));
System.out.println("aDouble = " + aDouble); // aDouble = 18800.286
//总和
Double aDouble1 = emps.stream().collect(Collectors.summingDouble(e -> e.getSalary()));
System.out.println("aDouble1 = " + aDouble1);//aDouble1 = 94001.43000000001
//最大值
Optional<Employee> max = emps.stream().collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary()
, e2.getSalary())));
System.out.println(max.get());
//Employee{name='李四', age=34, salary=26000.33, address='北京'}
//最小值
Optional<Employee> min = emps.stream().collect(Collectors.minBy((e1, e2) -> Double.compare(e1.getSalary()
, e2.getSalary())));
System.out.println(min.get());
//Employee{name='张三', age=23, salary=14000.11, address='深圳'}
}
/*
* 分组
* */
@Test
public void test4() {
//按照地址分组
Map<String,List<Employee>> map = emps.stream().collect(Collectors.groupingBy(Employee::getAddress));
System.out.println("map = " + map);
//map = {广州=[Employee{name='赵六', age=32, salary=22000.88, address='广州'}], 上海=[Employee{name='王五', age=55,
// salary=18000.0, address='上海'}], 北京=[Employee{name='李四', age=34, salary=26000.33, address='北京'}],
// 深圳=[Employee{name='张三', age=23, salary=14000.11, address='深圳'}, Employee{name='田七', age=23,
// salary=14000.11, address='深圳'}]}
}
/*
* 分区
* */
@Test
public void test5() {
Map<Boolean, List<Employee>> map = emps.stream().collect(Collectors.partitioningBy(e -> e.getSalary()>15000));
System.out.println("map = " + map);
//map = {false=[Employee{name='张三', age=23, salary=14000.11, address='深圳'}, Employee{name='田七', age=23, salary=14000.11, address='深圳'}], true=[Employee{name='李四', age=34, salary=26000.33, address='北京'}, Employee{name='王五', age=55, salary=18000.0, address='上海'}, Employee{name='赵六', age=32, salary=22000.88, address='广州'}]}
}
}
练习
package com.gl.testLambda;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Created by IntelliJ Idea 2018.1
* Author:Gavin Zhang
* Data: 2021-08-03
* Time: 21:42
*/
public class TestStreamApi5 {
List<TradeList> tradeLists = Arrays.asList(
new TradeList("唐僧","东土大唐","2008",2000),
new TradeList("孙悟空","花果山","2008",1500),
new TradeList("猪八戒","高老庄","2008",1000),
new TradeList("沙和尚","流沙河","2008",800),
new TradeList("刘备","蜀","2009",3000),
new TradeList("关羽","蜀","2009",2200),
new TradeList("张飞","蜀","2009",2000),
new TradeList("曹操","魏","2010",1500),
new TradeList("曹丕","魏","2010",1500)
);
/*
* 练习
* */
@Test
public void test1() {
//1.找出2008年发生的所有交易订单,并按照交易额从小到大排序
List<TradeList> collect = tradeLists.stream().filter(e -> e.getYear().equals("2008"))
.sorted((e1,e2)->Double.compare(e1.getTradeAmount(),e2.getTradeAmount()))
.collect(Collectors.toList());
System.out.println("collect = " + collect);
//collect = [TradeList{name='沙和尚', city='流沙河', year='2008', tradeAmount=800.0}, TradeList{name='猪八戒',
// city='高老庄', year='2008', tradeAmount=1000.0}, TradeList{name='孙悟空', city='花果山', year='2008',
// tradeAmount=1500.0}, TradeList{name='唐僧', city='东土大唐', year='2008', tradeAmount=2000.0}]
}
@Test
public void test2() {
//2.交易员都在哪些不同的城市
List<String> collect = tradeLists.stream().map(e -> e.getCity()).distinct()
.collect(Collectors.toList());
System.out.println("collect = " + collect);
//collect 有序 = [东土大唐, 花果山, 高老庄, 流沙河, 蜀, 魏]
Set<String> collect1 = tradeLists.stream().map(e -> e.getCity())
.collect(Collectors.toSet());
System.out.println("collect1 = " + collect1);
//collect1 无序 = [蜀, 花果山, 流沙河, 高老庄, 东土大唐, 魏]
}
@Test
public void test3() {
//3.查找出来自蜀的交易员,并按照姓名排序
List<String> list = tradeLists.stream().filter(t -> t.getCity().equals("蜀"))
.collect(Collectors.toList())
.stream().map(e -> e.getName()).sorted()
.collect(Collectors.toList());
System.out.println("list = " + list);
//list = [关羽, 刘备, 张飞]
}
@Test
public void test4() {
//4.有没有交易员是在魏工作的
boolean anyMatch = tradeLists.stream().anyMatch(e -> e.getCity().equals("蜀"));
System.out.println("anyMatch = " + anyMatch);
}
@Test
public void test5() {
//5.计算生活在蜀的交易员的所有交易额
Optional<Double> sum = tradeLists.stream().filter(e -> e.getCity().equals("蜀"))
.map(e->e.getTradeAmount())
.reduce(Double::sum);
System.out.println(sum.get());//7200.0
Optional<Double> sum2 = tradeLists.stream().filter(e -> e.getCity().equals("蜀"))
.collect(Collectors.toList())
.stream().map(e -> e.getTradeAmount())
.reduce(Double::sum);
System.out.println(sum2.get());//7200.0
}
@Test
public void test6() {
//6.所有交易中,最少/最高的交易额是多少
//最少
Optional<Double> min = tradeLists.stream().map(e -> e.getTradeAmount())
.sorted()
.collect(Collectors.toList())
.stream().findFirst();
System.out.println(min.get());//800.0
//最少
Optional<Double> min1 = tradeLists.stream().map(e -> e.getTradeAmount())
.min(Double::compareTo);
System.out.println(min1.get());//800.0
//最多
Optional<Double> max = tradeLists.stream().map(e -> e.getTradeAmount())
.max(Double::compareTo);
System.out.println(max.get());//3000.0
}
@Test
public void test7() {
//找到交易额最小的交易信息
Optional<TradeList> min = tradeLists.stream()
.min((e1, e2) -> Double.compare(e1.getTradeAmount(), e2.getTradeAmount()));
System.out.println(min.get());
//TradeList{name='沙和尚', city='流沙河', year='2008', tradeAmount=800.0}
Optional<TradeList> max = tradeLists.stream()
.max((e1, e2) -> Double.compare(e1.getTradeAmount(), e2.getTradeAmount()));
System.out.println(max.get());
//TradeList{name='刘备', city='蜀', year='2009', tradeAmount=3000.0}
}
}