zoukankan      html  css  js  c++  java
  • [集合操作]List对象数组获取元素值非空对象及根据对象元素值排序取最大&取对象数组的对象元素集合&条件去重&条件分组

    如果有个List如下图所示,我需要把这个List先把 url 为空的过滤,然后根据id分组,取date字段最大的对象,形成新的集合

    List data = [
      {"date":"2018-10-12", id:"1",url:"hdjf"},
      {"date":"2018-10-13", id:"1",url:"hdjf"}, 
      {"date":"2018-10-12", id:"2",url:"hdjf"},
      {"date":"2018-10-13", id:"2"},  
      {"date":"2018-10-12", id:"3",url:"hdjf"},
      {"date":"2018-10-13", id:"3"},       
    ]

    代码如下:

    Groovy:

    List eles = data.findAll {it.url != null}.groupBy {it.id}.collect {it -> it.value.max{it.date}}
    findAll过滤之后是个新的List,然后根据 id GroupBy 得到的是个如下的Map
    Map = [
    1:[{"date":"2018-10-12", id:"1",url:"hdjf"},{"date":"2018-10-13", id:"1",url:"hdjf"}],
    2:[{"date":"2018-10-12", id:"1",url:"hdjf"}],
    3:[{"date":"2018-10-12", id:"1",url:"hdjf"}]
    ]

    最后调用 collect 遍历Map ,每一项Item 获取group之后的最大的

    Java代码参考:

    import java.util.Arrays;
    import java.util.List;
    import java.util.Map;
    import java.util.Optional;
    import java.util.stream.Collectors;
    
    public class Main {
      public static void main(String...args){
        Map<Type, Food> o =  
        Food.menu.stream().collect(
            Collectors.groupingBy(Food::getType,
                Collectors.collectingAndThen(
                    Collectors.reducing((Food d1, Food d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2),
                        Optional::get)));
        
        System.out.println(o);
        
      }
    }
    enum Type { MEAT, FISH, OTHER }
    class Food {
    
      private final String name;
      private final boolean vegetarian;
      private final int calories;
      private final Type type;
    
      public Food(String name, boolean vegetarian, int calories, Type type) {
          this.name = name;
          this.vegetarian = vegetarian;
          this.calories = calories;
          this.type = type;
      }
    
      public String getName() {
          return name;
      }
    
      public boolean isVegetarian() {
          return vegetarian;
      }
    
      public int getCalories() {
          return calories;
      }
    
      public Type getType() {
          return type;
      }
      @Override
      public String toString() {
          return name;
      }
    
      public static final List<Food> menu =
              Arrays.asList( new Food("pork", false, 1800, Type.MEAT),
                             new Food("beef", false, 7100, Type.MEAT),
                             new Food("chicken", false, 1400, Type.MEAT),
                             new Food("french fries", true, 1530, Type.OTHER),
                             new Food("rice", true, 3510, Type.OTHER),
                             new Food("season fruit", true, 1120, Type.OTHER),
                             new Food("pizza", true, 5150, Type.OTHER),
                             new Food("prawns", false, 1400, Type.FISH),
                             new Food("salmon", false, 4150, Type.FISH));
    }

     第二:取对象数组中对象元素的集合

    public static void main(String[] args) {
            List<Student> stuList = new ArrayList<Student>();
            Student st1 = new Student("123","aaa");
            Student st2 = new Student("234","bbb");
            Student st3 = new Student("345","ccc");
            Student st4 = new Student("345","ccc");
            stuList.add(st1);
            stuList.add(st2);
            stuList.add(st3);
            stuList.add(st4);
            //1.提取出list对象中的一个属性
            List<String> stIdList1 = stuList.stream()
                    .map(Student::getId)
                    .collect(Collectors.toList());
            stIdList1.forEach(s -> System.out.print(s+" "));
            System.out.println();
            System.out.println("----------");
            
            //2.提取出list对象中的一个属性并去重
            List<String> stIdList2 = stuList.stream()
                    .map(Student::getId).distinct()
                    .collect(Collectors.toList());
            stIdList2.forEach(s -> System.out.print(s+" "));
            /*    结果:
                123 234 345 345  
                ----------
                123 234 345 
            */
        }

    第三 条件去重

    普通去重  distinct()函数能够根据简单类型直接去重

    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 5, 5, 5, 6, 7);
     
    List<Integer> distinctNumbers = numbers.stream()
    .distinct()
    .collect(Collectors.toList());
    System.out.println(distinctNumbers);//1, 2, 3, 4, 5, 6, 7

    如果需要对类型List<User>去重

    首先定义一个过滤器:
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return object -> seen.putIfAbsent(keyExtractor.apply(object), Boolean.TRUE) == null;
    }
    List<User> users = new LinkedList<>();
    users.add(new User("Jim"));
    users.add(new User("Jim"));
    users.add(new User("Tom"));
    users.add(new User("Leo"));
    //愉快的去重
    List<User> distinctUsers = users.stream()
            .filter(distinctByKey(User::getName))
            .collect(Collectors.toList());
    
    System.out.println(distinctUsers);//[Jim, Tom, Leo]

    四。条件分组

    List<User> users = new LinkedList<>();
    users.add(new User("Jim", 12));
    users.add(new User("John", 18));
    users.add(new User("Tom", 21));
    users.add(new User("Leo", 30));
    users.add(new User("Kate", 44));
    users.add(new User("Lio", 50));
     
    Map<String, List<User>> tripleUsers = users.stream()
            .collect(Collectors.groupingBy((Function<User, String>) user -> {
        String key;
        if (user.getAge() <= 20) {
            key = "less20";
        } else if (user.getAge() <= 40) {
            key = "less40";
        } else {
            key = "more40";
        }
        return key;
    }, Collectors.toList()));
     
    System.out.println(tripleUsers);
    //{more40=[Kate, Lio], less40=[Tom, Leo], less20=[Jim, John]}

    //根据年龄分组

    Map<String, List<User>> studlistGrouped = studlist.stream().collect(Collectors.groupingBy(w -> w.age));
     
    欢迎关注Java流水账公众号
  • 相关阅读:
    BZOJ-1497 [NOI2006]最大获利 最小割
    BZOJ-2768 [JLOI2010]冠军调查 最小割
    BZOJ-3504 [Cqoi2014]危桥 最大流
    BZOJ-3894 文理分科 最小割
    HDU5196--DZY Loves Inversions 树状数组 逆序数
    【2013南京区域赛】部分题解 hdu4802—4812
    POJ1741--Tree (树的点分治) 求树上距离小于等于k的点对数
    SPOJ694 -- DISUBSTR 后缀树组求不相同的子串的个数
    POJ1743---Musical Theme (后缀数组+二分)
    POJ3729 Facer’s string 后缀数组
  • 原文地址:https://www.cnblogs.com/guofu-angela/p/10028929.html
Copyright © 2011-2022 走看看