Stream + Lambda 表达式

去重

  • 两个list对象根据条件去重
List<User> userList 和 List<Person> personList

// 去掉 userList 中的 User 的 id 不等于 personList 中的 Person 的 id 的对象
List<User> userList = userList.stream()
    .filter(user -> !personList.stream().map(person - >person.getId()).collect(Collectors.toList())
    .contains(user.getId()))
    .collect(Collectors.toList());
  • 去重
 // 单条件去重   
list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                -> new TreeSet<>(Comparator.comparing(user -> user .getId()))), ArrayList::new));  

// 多条件升级
list.stream().collect(Collectors.collectingAndThen(
        Collectors.toCollection(() -> new TreeSet<>(
                Comparator.comparing(user -> user.getAge() + ";" + user.getName()))), ArrayList::new))                
  • 简单List集合求交、去重、差集
// 交集 (parallelStream() 使用并行流遍历,多线程; stream() )
List<String> intersection = list1.stream()
    .filter(item -> list2.contains(item))
    .collect(toList());

// 差集 (list1 - list2)
List<String> reduce1 = list1.stream()
    .filter(item -> !list2.contains(item))
    .collect(toList());

// 差集 (list2 - list1)
List<String> reduce2 = list2.stream()
    .filter(item -> !list1.contains(item))
    .collect(toList());

// 去重
List<String> listAllDistinct = listAll.stream()
    .distinct()
    .collect(toList());

排序

List<Person> sortedList = list.stream()
    .sorted((o1,o2) -> o1.getAge() - o2.getAge())
    .collect(Collectors.toList());
    
List<Person> sortedList =  list.stream()
.sorted(Comparator.comparing(ClassImagePraise::getSort))
.collect(Collectors.toList());    

List<Person> sortedList =  list.stream()
.sorted(Comparator.comparing(ClassImagePraise::getSort)).reversed())
.collect(Collectors.toList()); 

// 多条件组合排序
// 先根据msg(升序),再根据id(升序)
list.stream()
    .sorted(Comparator.comparing(Message::getMsg)
    .thenComparing(Message::getId))
    .collect(Collectors.toList()); 

// 根据msg(升序),再根据id(降序)
list.stream()
    .sorted(Comparator.comparing(Message::getMsg)
    .thenComparing(Comparator.comparing(Message::getId).reversed()))
    .collect(Collectors.toList()); 

// 先根据msg(降序),再根据id(降序)
list.stream()
    .sorted(Comparator.comparing(Message::getMsg)
    .thenComparing(Message::getId).reversed())
    .collect(Collectors.toList()); 
    
// 先根据msg(降序),再根据id(升序)
list.stream()
    .sorted(Comparator.comparing(Message::getMsg).reversed()
    .thenComparing(Message::getId))
    .collect(Collectors.toList()); 

过滤

List<Person> filterList = list.stream()
    .filter(item -> item.getAge() > 3)
    .collect(Collectors.toList());

筛选、转换

List<String> mapList1 = list.stream()
    .map(Person::getName)
    .collect(Collectors.toList());
    
List<String> mapList2 = list.stream()
    .map(item -> item.getName())
    .collect(Collectors.toList());
    
// 过滤与筛选结合
List<String> teacherIds = pList.stream()
    .filter(s -> s.getId().equals("333"))
    .map(p -> p.getUserId())
    .collect(Collectors.toList());
    
// .collect(Collectors.toMap(LogAnalystDTO::getRequestDate, LogAnalystDTO::getRequestDateNum)) 

统计

// getAverage统计 sum() .mapToDouble()转换成double。还有其他类型转换。可以自己研究。max(),min(),average()

double sum = list.stream().mapToDouble(Person::getAge).sum();

Optional<Message> maxMassage = list.stream()
    .collect(Collectors.maxBy(Comparator.comparing(Message::getId)));
Long maxId = maxMassage.get().getId();

LongSummaryStatistics lss = list.stream()
    .collect(Collectors.summarizingLong(Message::getId));

//  求和 lss.getSum()
//  求最大值 lss.getMax()
// 求最小值 lss.getMin()
// 求平均值 lss.getAverage()

分组

Map<Integer, List<Person>> map = list.stream()
    .collect(Collectors.groupingBy(Person::getAge));
    
// 多重分组
Map<String, Map<Integer, List<Person>>> map2 = list.stream()
    .collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge())));
    
// 分组并计算综合        Collectors.summarizingLong()
Map<String, Map<Integer, LongSummaryStatistics>> map3 = list.stream()
    .collect(Collectors.groupingBy(t->t.getName(),Collectors.groupingBy(t->t.getAge(),Collectors.summarizingLong(Person::getSize))));

遍历

items.forEach(item->{
    System.out.println(item);
});
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值