zoukankan      html  css  js  c++  java
  • Stream的去重排序

    1.List<Integer>排序

    List<Integer> list = new ArrayList<>();
    list.add(50);
    list.add(25);
    list.add(25);
    list.add(98);
    list.add(32);
    List<Integer> collect = list.stream().distinct().sorted().collect(Collectors.toList());
    System.out.println("去重正序" + collect);
    List<Integer> collect1 = list.stream().distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    System.out.println("去重倒序" + collect1);

    打印结果
    去重正序[25, 32, 50, 98]
    去重倒序[98, 50, 32, 25]

     2.List<Person>的元素的属性值去重

    
    
    Person p1 = new Person("张三", new BigDecimal("50.0"));
    Person p2 = new Person("王五", new BigDecimal("25.0"));
    Person p3 = new Person("李四", new BigDecimal("68.0"));
    Person p4 = new Person("赵六", new BigDecimal("17.0"));
    Person p5 = new Person("马八", new BigDecimal("25.0"));
    List<Person> list = new ArrayList<>();
    list.add(p1);
    list.add(p2);
    list.add(p3);
    list.add(p4);
    list.add(p5);
    List<Person> result = new LinkedList<>();
    for (Person person : list) {
    boolean b = result.stream().anyMatch(p -> p.getSalary().compareTo(person.getSalary())==0);
    if (!b) {
    result.add(person);
    }
    }
    System.out.println(result);

    打印结果:
    [{name='张三', salary=50.0}, {name='王五', salary=25.0}, {name='李四', salary=68.0}, {name='赵六', salary=17.0}]
  • 相关阅读:
    iOS CALayer 学习(2)
    iOS CALayer 学习(1)
    iOS 绘画学习(5)
    iOS 绘画学习(4)
    果冻视图制作教程
    15个名不见经传的Unix命令
    WEB服务器2--IIS架构(转)
    WEB服务器1--开篇
    HTTP协议5之代理--转
    HTTP协议4之缓存--转
  • 原文地址:https://www.cnblogs.com/lu51211314/p/9378332.html
Copyright © 2011-2022 走看看