zoukankan      html  css  js  c++  java
  • 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)); 2. 单列求和 int totalValue = details.stream().mapToInt(SendDetail::getSmsFee).sum(); 3. 提取单列数据集合 List<Integer> ids = details.stream().map(SendDetail::getId).collect(Collectors.toList()); 3人点赞 Java 作者: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
  • 相关阅读:
    7段数码管绘制
    画五角星
    绘制正方形
    蟒蛇的绘制
    玫瑰花
    小猪佩奇
    数列求和
    水仙花数
    鸡兔同笼
    画国际象棋盘
  • 原文地址:https://www.cnblogs.com/lanliying/p/15624503.html
Copyright © 2011-2022 走看看