• 【集合常用的方法】


    1.List判空二种方法:

    1.list != null && list.size() != 0;
    2.CollectionUtils.isEmpty(list);
    

    2. 获取list某个对象属性值的和

            //第一种 计数
            long sum1= list.stream().mapToLong(Entity::getId).sum();
            list.stream().mapToInt/mapDouble//数据类型不同
            //第二种 技术
            LongSummaryStatistics statistics= list.stream().mapToLong(entity -> entity .getId())).summaryStatistics();
            long sum2 = statistics.getSum();
            //其他常用的方法
            statistics.getMax();//最大值
            statistics.getMin();最小值
            statistics.getAverage();平均值
    

    3. 根据list中对象某个属性值分组

    //分组
    Map<String, List<Entity>> groupMap=list.stream().collect(Collectors.groupingBy(Entity::getId));
    //计数
    Map<String, Long> countMap=list.stream().collect(Collectors.groupingBy(Entity::getId,Collectors.counting()));
    //分组并根据另一个属性计数
    Map<String, Long> countMap=list.stream().collect(Collectors.groupingBy(Entity::getId,Collectors.summingInt(Entity::getPrice)));
    

    4. 将list转换为map

    //第一种
     list.stream().collect(Collectors.toMap(Entity::getId, Entity::getName));
    //第二种
     list.stream().collect(Collectors.toMap(t -> t.getId()+"#"+t.getPrice(),p -> p.getName())
    
  • 相关阅读:
    开关门(结构体)
    洗牌问题(找规律)
    七夕节(hd1215)干嘛今天做这题T_T
    三角形(hd1249)
    寒冰王座(hd1248)
    钱币兑换问题(hd1284)
    计算机模拟(hd1283)
    回文数猜想(hd1282)
    贪吃蛇代码
    变形课hd1181(DFS)
  • 原文地址:https://www.cnblogs.com/linanana/p/16379251.html
Copyright © 2020-2023  润新知