JDK stream常用记录


        List<Store> storeList = new ArrayList<>();
        storeList.add(new Store(1L,"name1"));
        storeList.add(new Store(2L,"name2"));
        storeList.add(new Store(3L,"name3"));
        storeList.add(new Store(4L,"name4"));
        storeList.add(new Store(5L,"name5"));
        storeList.add(new Store(6L,"name6"));
        storeList.add(new Store(7L,"name7"));
        storeList.add(new Store(7L,"name7"));

        // 1. 分组
        Map<String, List<Store>> storeMap = storeList.stream().collect(Collectors.groupingBy(Store::getStoreName));
        Map<String, List<Long>> idMap = storeList.stream().collect(Collectors.groupingBy(Store::getStoreName, Collectors.mapping(Store::getStoreId, Collectors.toList())));
        // 2. 获取最大
        Store store = storeList.stream().max(Comparator.comparing(Store::getStoreId)).get();

        //3. 过滤list
        List<Store> collect = storeList.stream().filter(e -> "name7".equals(e.getStoreName())).collect(Collectors.toList());

        Map<Long,Store> storeMap1 = new HashMap<>();

        storeList.forEach(e->storeMap1.put(e.getStoreId(),e));

        //3. 过滤map
        List<Long> collect1 = storeMap1.keySet().stream().filter(e -> 7L == e).collect(Collectors.toList());
        Map<Long, Store> collect2 = storeMap1.entrySet().stream().filter(e -> 7L == e.getKey()).collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值