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);
});
posted @ 2020-01-02 15:05  我是我最后的目击者  阅读(295)  评论(0编辑  收藏  举报