用Stream流处理重复数据,多种方式去重list,可整体去重或按某个属性去重

1、String 集合去重,例如List<String>

@Test
public void listDistinctByStreamDistinct() {
    //数据准备
    List<String> strList = new ArrayList<String>() {{
        add("A");
        add("A");
        add("B");
        add("B");
        add("C");
    }};

    //控制台打印原始数据
    System.out.println("去重前:");
    for (String s : strList) {
         System.out.println(s);
    }
    
    List<String> collectList = strList.stream().distinct().collect(Collectors.toList());
    
    //控制台打印原始数据    
    System.out.println("去重后:");
    for (String s : collectList) {
        System.out.println(s);
    }
}

//控制台日志如下:
/*
    去重前:AABBC
    去重后:ABC
*/

2、实体对象集合去重,例如List<Student>

/**
 * 定义一个实体类
 */ 
@Data
public class Student {
  private String stuNo;
  private String name;
}


@Test
public void listDistinctByStreamDistinct() throws Exception {
    //假设List<Student>数据已经准备好了,命名为studentList

    System.out.println("去重前:/n");
    System.out.println(studentList);

    List<Student> coList = studentList.stream().distinct().collect(Collectors.toList());

    System.out.println("去重后:/n");
    System.out.println(coList);
  }

//控制台日志
/*
    去重前:
            [
               {"stuNo":"001","name":"Tom"},
               {"stuNo":"002","name":"Mike"},
               {"stuNo":"001","name":"Tom"}
            ]
    去重后:
            [
                {"stuNo":"001","name":"Tom"},
                {"stuNo":"002","name":"Mike"}
            ]
*/

3. 根据 List<Object> 中 Object 的某个属性去重

/filter去重方法实现类
private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    Set<Object> seen = ConcurrentHashMap.newKeySet();
    return t -> seen.add(keyExtractor.apply(t));
}

  
@Test
public void distinctByProperty1() throws Exception {
    System.out.println("去重前:/n");
    System.out.println(studentList);
    
    //根据对象去重
    List<Student> coList = studentList.stream().distinct().collect(Collectors.toList());

    System.out.println("去重后:/n");
    System.out.println(coList);

    //根据对象的属性去重1
    List<Student> nList1 = studentList.stream().collect(collectingAndThen(
                               toCollection(() -> new 
                               TreeSet<(Comparator.comparing(Student::getName))),
                               ArrayList::new));

    //根据对象的属性去重2
    List<Student> nList2 = studentList.stream().filter(distinctByKey(Student::getName))
                                     .collect(Collectors.toList());

    System.out.println("方法1根据name属性去重后:/n");
    System.out.println(nList1);
    System.out.println("方法2根据name属性去重后:/n");
    System.out.println(nList2);
}

//控制台打印
/*
    去重前:
            [
                {"stuNo":"001","name":"Tom"},
                {"stuNo":"001","name":"Tom"},    
                {"stuNo":"003","name":"Tom"}
            ]
    去重后:
            [
                {"stuNo":"001","name":"Tom"},
                {"stuNo":"003","name":"Tom"}
            ]
    方法1根据name属性去重后:
            [
                {"stuNo":"001","name":"Tom"}
            ]
    方法2根据name属性去重后:
            [
                {"stuNo":"001","name":"Tom"}
            ]
*/
  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值