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
    评论
streamlambda表达式是Java 8中引入的新特性,用于简化集合操作的代码。stream是一个顺序流或并行流,可以对集合中的元素进行各种操作,如过滤、转换、排序等,而不需要显式使用迭代器或循环。 lambda表达式是一种匿名函数,可以像一般的方法一样传递给其他方法或函数接口中使用。它可以简化代码的书写,并且使代码更易读和维护。通过lambda表达式,可以更加灵活地编写函数式接口的实现。 在练习文件"streamlambda表达式练习.doc"中,我可以想到以下可能的练习内容: 1. 使用streamlambda表达式对一个整数集合进行过滤,只保留偶数。 2. 使用streamlambda表达式对一个字符串集合进行转换,将每个字符串转换为大写。 3. 使用streamlambda表达式对一个学生对象集合进行排序,按照姓名字母顺序排序。 4. 使用streamlambda表达式计算一个整数集合中所有元素的平均值。 5. 使用streamlambda表达式对一个字符串集合进行去重,保留唯一的字符串。 这些练习可以帮助我们熟悉streamlambda表达式的使用,并且体会到它们的便利之处。在实际的开发工作中,streamlambda表达式能够大大简化我们的代码,并提升我们的开发效率。同时,它们也是函数式编程的一种体现,使得我们可以更加灵活地处理集合中的元素,提高代码的可读性和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值