复习jdk1.8流的使用
1 创建一个集合
List<Order> orderList = new ArrayList<>();
orderList.add(Order.builder().buyerName("王龙").state(1).amount(new BigDecimal(3.8)).orderNo("5438111").build());
orderList.add(Order.builder().buyerName("王龙").state(0).amount(new BigDecimal(5.2)).orderNo("5438112").build());
orderList.add(Order.builder().buyerName("王龙星").state(1).amount(new BigDecimal(5.2)).orderNo("5438113").build());
orderList.add(Order.builder().buyerName("王龙星").state(0).amount(new BigDecimal(3.8)).orderNo("5438114").build());
orderList.add(Order.builder().buyerName("王龙").state(1).amount(new BigDecimal(7.8)).orderNo("5438115").build());
2 过滤
List<String> buyerNameList =
orderList.stream().filter(order -> "王龙".equals(order.getBuyerName())).map(Order::getBuyerName).collect(Collectors.toList());
buyerNameList.forEach(System.out::println);
// 输出
王龙
王龙
王龙
2 映射
a 去重
List<String> distinctBuyerName =
orderList.stream().map(Order::getBuyerName).distinct().collect(Collectors.toList());
distinctBuyerName.forEach(System.out::println);
// 输出
王龙
王龙星
b 跳过
int skippedSize = (int) orderList.stream().skip(4).count();
System.out.println("skippedSize="+ skippedSize);
// 输出
skippedSize=1
c 限定
long limitCount = orderList.stream().limit(2).count();
System.out.println("limitCount="+ limitCount);
// 输出
limitCount=2
3 终端操作
a 查找
Optional<Order> anyOrder = orderList.stream().findAny().filter(order -> "王龙".equals(order.getBuyerName()));
anyOrder.ifPresent(order -> System.out.println(order.getBuyerName() + ":" + order.getOrderNo()));
Optional<Order> firstOrder = orderList.stream().findFirst().filter(order -> "王龙".equals(order.getBuyerName()));
firstOrder.ifPresent(order -> System.out.println(order.getBuyerName() + ":" + order.getOrderNo()));
//输出
王龙:5438111
王龙:5438111
b 规约
double totalAmount1 = orderList.stream().mapToDouble(order -> order.getAmount().doubleValue()).sum();
double totalAmount2 = orderList.stream().mapToDouble(order -> order.getAmount().doubleValue()).reduce(0.0D, Double::sum);
System.out.println("totalAmount1="+ totalAmount1);
System.out.println("totalAmount2="+ totalAmount2);
OptionalDouble totalAmountOptional =
orderList.stream().mapToDouble(order -> order.getAmount().doubleValue()).reduce( Double::sum);
totalAmountOptional.ifPresent(totalAmount -> System.out.println("totalAmount3="+ totalAmount));
//输出
totalAmount1=25.8
totalAmount2=25.8
totalAmount3=25.8
c 分组
Map<String, List<Order>> buyerNameGroupMap = orderList.stream().collect(Collectors.groupingBy(Order::getBuyerName));
Set<String> buyerNameGroup = buyerNameGroupMap.keySet();
buyerNameGroup.forEach(System.out::println);
//输出
王龙
王龙星
d 分区
Map<Boolean, List<Order>> partitionMap =
orderList.stream().collect(Collectors.partitioningBy(order -> "王龙".equals(order.getBuyerName())));
for(Map.Entry<Boolean, List<Order>> entry: partitionMap.entrySet()){
System.out.println(entry.getKey());
entry.getValue().forEach(System.out::print);
System.out.println(" ");
}
//输出
false
Order(orderNo=5438113, state=1, amount=5.20000000000000017763568394002504646778106689453125, buyerName=王龙星)Order(orderNo=5438114, state=0, amount=3.79999999999999982236431605997495353221893310546875, buyerName=王龙星)
true
Order(orderNo=5438111, state=1, amount=3.79999999999999982236431605997495353221893310546875, buyerName=王龙)Order(orderNo=5438112, state=0, amount=5.20000000000000017763568394002504646778106689453125, buyerName=王龙)Order(orderNo=5438115, state=1, amount=7.79999999999999982236431605997495353221893310546875, buyerName=王龙)