jdk8流的基础使用

复习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=王龙)         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值