博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA8-用lamda表达式和增强版Comparator进行排序(转)
阅读量:4042 次
发布时间:2019-05-24

本文共 2378 字,大约阅读时间需要 7 分钟。

转自:

以前的排序一般对象实现Comparable或者Comparator接口,经常是通过匿名类类实现。

可以参见以前的博文 Java 中 Comparable 和 Comparator 比较
现在看看使用lamda表达式和java8中增强的Comparator接口进行排序。

先定义一个简单的实体类:

class Human {    private String name;    private int age;    public Human() {        super();    }    public Human(final String name, final int age) {        super();        this.name = name;        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

1. 使用lamda表达式或方法引用进行排序

lamda表达式

List
humans = new ArrayList<>();humans.add(new Human("Sarah", 10));humans.add(new Human("Jack", 12));humans.sort((Human h1, Human h2) -> h1.getName().compareTo(h2.getName()));//humans.sort((h1, h2) -> h1.getName().compareTo(h2.getName()));//简化
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

当然lamda表达式也可以使用静态方法的引用代替。

public static int compareByNameThenAge(Human lhs, Human rhs) {    if (lhs.name.equals(rhs.name)) {        return lhs.age - rhs.age;    } else {        return lhs.name.compareTo(rhs.name);    }}List
humans = new ArrayList<>();humans.add(new Human("Sarah", 10));humans.add(new Human("Jack", 12));humans.sort(Human::compareByNameThenAge);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

2. 使用增强版的Comparator接口

List
humans = new ArrayList<>();humans.add(new Human("Sarah", 10));humans.add(new Human("Jack", 12));Collections.sort(humans, Comparator.comparing(Human::getName));
  • 1
  • 2
  • 3
  • 4
  • 5

还可以使用反转

Collections.sort(humans, Comparator.comparing(Human::getName).reversed());
  • 1

3. 多条件排序

3.1 使用lamda表达式

List
humans = new ArrayList<>();humans.add(new Human("Sarah", 10));humans.add(new Human("Jack", 12));humans.add(new Human("Jack", 10));humans.sort((lhs, rhs) -> { if (lhs.getName().equals(rhs.getName())) { return Integer.compare(lhs.getAge(), rhs.getAge()); } else { return lhs.getName().compareTo(rhs.getName()); } });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3.2使用Comparator进行组合

List
humans = new ArrayList<>();humans.add(new Human("Sarah", 10));humans.add(new Human("Jack", 12));humans.add(new Human("Jack", 10));humans.sort(Comparator.comparing(Human::getName).thenComparing(Human::getAge));

转载地址:http://rradi.baihongyu.com/

你可能感兴趣的文章
Maven跳过单元测试的两种方式
查看>>
通过C++反射实现C++与任意脚本(lua、js等)的交互(二)
查看>>
利用清华镜像站解决pip超时问题
查看>>
[leetcode BY python]1两数之和
查看>>
微信小程序开发全线记录
查看>>
PTA:一元多项式的加乘运算
查看>>
CCF 分蛋糕
查看>>
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[leetCode By Python] 14. Longest Common Prefix
查看>>
[LeetCode By Python]118. Pascal's Triangle
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]122. Best Time to Buy and Sell Stock II
查看>>
[LeetCode By Python]125. Valid Palindrome
查看>>
[LeetCode By Python]136. Single Number
查看>>