Java 8 List转Map,分组,过滤

tech2024-03-24  18

先创建一个实体

private Integer id; /** * 登录账号 */ private String loginName; /** * 登录密码 */ private String password; /** * 当前密码 */ private String currentPassword; public TheaterDTO(Integer id, String loginName, String password, String currentPassword) { this.id = id; this.loginName = loginName; this.password = password; this.currentPassword = currentPassword; }

 

List<TheaterDTO> appleList = new ArrayList<>();//存放apple对象集合 TheaterDTO apple1 = new TheaterDTO(1,new BigDecimal("12"), "444", "1231", "qeweq"); TheaterDTO apple12 = new TheaterDTO(1,new BigDecimal("12"), "333", "1231", "123"); TheaterDTO apple2 = new TheaterDTO(2,new BigDecimal("12"), "111", "1231", "333"); TheaterDTO apple3 = new TheaterDTO(4, new BigDecimal("12"),"111", "1231", "qeweq"); appleList.add(apple1); appleList.add(apple12); appleList.add(apple2); appleList.add(apple3); // 1. 分组 List里面的对象元素,以某个属性来分组,例如,以id分组,将id相同的放在一起 //List 以ID分组 Map<Integer,List<Apple>> Map<Integer, List<TheaterDTO>> groupBy = appleList.stream().collect(Collectors.groupingBy(TheaterDTO::getId)); System.err.println("groupBy:" + groupBy); // {1=[Apple{id=1, name='苹果1', money=3.25, num=10}, Apple{id=1, name='苹果2', money=1.35, num=20}], 2=[Apple{id=2, name='香蕉', money=2.89, num=30}], 3=[Apple{id=3, name='荔枝', money=9.99, num=40}]} /** * List -> Map * 需要注意的是: * toMap 如果集合对象有重复的key,会报错Duplicate key .... * apple1,apple12的id都为1。 * 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2 * 需要注意get 保持唯一 否者只展示Key1 * List转Map * id为key,apple对象为value */ Map<Integer, TheaterDTO> appleMap = appleList.stream().collect(Collectors.toMap(TheaterDTO::getId, a -> a, (k1, k2) -> k1)); System.out.println(appleMap); // 过滤Filter //从集合中过滤出来符合条件的元素: // 过滤出符合条件的数据 List<TheaterDTO> filterList = appleList.stream().filter(a -> a.getCurrentPassword().equals("qeweq")).collect(Collectors.toList()); System.err.println("filterList:" + filterList); //计算 总金额 将集合中的数据按照某个属性求和 BigDecimal totalMoney = appleList.stream().map(TheaterDTO::getPrice).reduce(BigDecimal.ZERO, BigDecimal::add); System.err.println("totalMoney:"+totalMoney); //totalMoney:17.48 // 根据id去重 List<TheaterDTO> unique = appleList.stream().collect( collectingAndThen( toCollection(() -> new TreeSet<>(comparingLong(TheaterDTO::getId))), ArrayList::new) ); System.out.println(unique); 工厂方法返回类型作用toListList<T>把流中所有项目收集到一个 ListtoSetSet<T>把流中所有项目收集到一个 Set,删除重复项toCollectionCollection<T>把流中所有项目收集到给定的供应源创建的集合menuStream.collect(toCollection(), ArrayList::new)countingLong计算流中元素的个数sumIntInteger对流中项目的一个整数属性求和averagingIntDouble计算流中项目 Integer 属性的平均值summarizingIntIntSummaryStatistics收集关于流中项目 Integer 属性的统计值,例如最大、最小、 总和与平均值joiningString连接对流中每个项目调用 toString 方法所生成的字符串collect(joining(", "))maxByOptional<T>一个包裹了流中按照给定比较器选出的最大元素的 Optional, 或如果流为空则为 Optional.empty()minByOptional<T>一个包裹了流中按照给定比较器选出的最小元素的 Optional, 或如果流为空则为 Optional.empty()reducing归约操作产生的类型从一个作为累加器的初始值开始,利用 BinaryOperator 与流 中的元素逐个结合,从而将流归约为单个值累加int totalCalories = menuStream.collect(reducing(0, Dish::getCalories, Integer::sum));collectingAndThen转换函数返回的类型包裹另一个收集器,对其结果应用转换函数int howManyDishes = menuStream.collect(collectingAndThen(toList(), List::size))groupingByMap<K, List<T>>根据项目的一个属性的值对流中的项目作问组,并将属性值作 为结果 Map 的键partitioningByMap<Boolean,List<T>>根据对流中每个项目应用谓词的结果来对项目进行分区   

 

 

最新回复(0)