[集合操作]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));
 

 

转载于:https://www.cnblogs.com/zhengwangzw/p/10028929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值