悠然哈哈哈

导航

java 8 新特性

JAVA8新特性之List的各种用法(最大、最小、平均值、分组、求和、遍历、过滤、排序)

https://www.cnblogs.com/Kevin-ZhangCG/p/14918181.html

//day 为user的一个属性  去重
List<User> setList = eachUser.stream()
.collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getDay))), ArrayList::new));

原文链接:https://blog.csdn.net/xiaohanguo_xiao/article/details/105656411

 

例如说:(“s1”, 1, 1),(“s1”,2,3),(“s2”,4,4), 求和并去重的话,就是(“s1”, 3, 4),(“s2”,4,4)

List<OrderLine> orderLines = param.getOrderLine();

//合并sku数量
orderLines = new ArrayList<>(orderLines.stream()
// 表示id为key, 接着如果有重复的,那么从BillsNums对象o1与o2中筛选出一个,这里选择o1,
// 并把id重复,需要将nums和sums与o1进行合并的o2, 赋值给o1,最后返回o1
.collect(Collectors.toMap(OrderLine::getProductId, a -> a, (o1, o2) -> {
double qty = o1.getQuantity() ==null ? 0.0: o1.getQuantity();
double qty2 = o2.getQuantity() ==null ? 0.0: o2.getQuantity();
o1.setQuantity(qty + qty2);
return o1;
})).values());
参考:https://blog.csdn.net/qq_42928918/article/details/109763353



1. 分组 Map<String, List<SendDetail>> collect = details.stream().collect(Collectors.groupingBy(SendDetail::getCustomerId));
Map<String,WmHeader> map = headerList.stream().collect(Collectors.toMap(item -> String.join("_",item.getWareNo(),item.getLogNo()),item -> item, (k1, k2) -> k1));

Map<String, List<InvDetail>> listMap = invDetails.stream().collect(Collectors.groupingBy(lotLoc -> String.join("_",lotLoc.getCode() ,lotLoc.getTrId())));

2. 单列求和 int totalValue = details.stream().mapToInt(SendDetail::getSmsFee).sum();

3. 提取单列数据集合 List<Integer> ids = details.stream().map(SendDetail::getId).collect(Collectors.toList());
作者:hisenyuan 链接:https:
//www.jianshu.com/p/c71eaeaaf30c
List<User> userList = new ArrayList<>();
        userList.add(new User(1L, "zhangsan", 23, new BigDecimal(55)));
        userList.add(new User(2L, "lisi", 33, new BigDecimal(22)));
        userList.add(new User(3L, "wangwu", 44, new BigDecimal(44)));
        userList.add(new User(4L, "mazi", null, new BigDecimal(44)));

        // 对年龄进行统计
        int ageCount = userList.stream()
                .mapToInt(item -> item.getAge() == null ? 0 : item.getAge())
                .sum();
        System.out.println("年龄总和: " + ageCount);
        //对资产进行统计
        BigDecimal amounts1 = userList
                .stream()
                .map(item -> item.getAmount() == null ? BigDecimal.ZERO : item.getAmount())
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("资产总和1 :" + amounts1);

————————————————
原文链接:https://blog.csdn.net/qq_35098526/article/details/88186797

 

posted on 2021-11-30 15:32  悠然886  阅读(57)  评论(0编辑  收藏  举报