# Java函数式编程(十一)Comparator

Comparator接口的身影在JDK库中随处可见，从查找到排序，再到反转操作，等等。Java 8里它变成了一个函数式接口，这样的好处就是我们可以使用流式语法来实现比较器了。

```public class Person {
private final String name;
private final int age;
public Person(final String theName, final int theAge) {
name = theName;
age = theAge;
}
public String getName() { return name; }
public int getAge() { return age; }
public int ageDifference(final Person other) {
return age - other.age;
}
public String toString() {
return String.format("%s - %d", name, age);
}
}
```

```final List<Person> people = Arrays.asList(
new Person("John", 20),
new Person("Sara", 21),
new Person("Jane", 21),
new Person("Greg", 35));
```

```List<Person> ascendingAge =
people.stream()
.sorted((person1, person2) -> person1.ageDifference(person2))
.collect(toList());
printPeople("Sorted in ascending order by age: ", ascendingAge);
```

Comparator的抽象方法compareTo()接收两个参数，也就是要比较的对象，并返回一个int类型的结果。为了兼容这个，我们的lambda表达式也接收两个参数，两个Person对象，它们的类型是由编译器自动推导的。我们返回一个int类型，表明比较的对象是否相等。

sorted()方法会遍历目标集合的每个元素并调用指定的Comparator，来确定出元素的排序顺序。sorted()方法的执行方式有点类似前面说到的reduce()方法。reduce()方法把列表逐步归约出一个结果。而sorted()方法则通过比较的结果来进行排序。

```public static void printPeople(
final String message, final List<Person> people) {
System.out.println(message);
people.forEach(System.out::println);
}
```

```Sorted in ascending order by age:
John - 20
Sara - 21
Jane - 21
Greg - 35
```

```.sorted((person1, person2) -> person1.ageDifference(person2))
```

```people.stream()
.sorted(Person::ageDifference)
```

```printPeople("Sorted in descending order by age: ",
people.stream()
.sorted((person1, person2) -> person2.ageDifference(person1))
.collect(toList()));
```

```Sorted in descending order by age:
Greg - 35
Sara - 21
Jane - 21
John - 20
```

```Comparator<Person> compareAscending =
(person1, person2) -> person1.ageDifference(person2);
Comparator<Person> compareDescending = compareAscending.reversed();
```

```printPeople("Sorted in ascending order by age: ",
people.stream()

.sorted(compareAscending)

.collect(toList())
);
printPeople("Sorted in descending order by age: ",
people.stream()
.sorted(compareDescending)
.collect(toList())
);
```

```printPeople("Sorted in ascending order by name: ",
people.stream()
.sorted((person1, person2) ->
person1.getName().compareTo(person2.getName()))
.collect(toList()));
```

```Sorted in ascending order by name:
Greg - 35
Jane - 21
John - 20
Sara - 21
```

```people.stream()
.min(Person::ageDifference)
.ifPresent(youngest -> System.out.println("Youngest: " + youngest));
```

```Youngest: John - 20
```

```people.stream()
.max(Person::ageDifference)
.ifPresent(eldest -> System.out.println("Eldest: " + eldest));
```

```Eldest: Greg - 35
```

```people.stream()
.sorted((person1, person2) ->
person1.getName().compareTo(person2.getName()));
```

```final Function<Person, String> byName = person -> person.getName();
people.stream()
.sorted(comparing(byName));
```

```final Function<Person, Integer> byAge = person -> person.getAge();
final Function<Person, String> byTheirName = person -> person.getName();
printPeople("Sorted in ascending order by age and name: ",
people.stream()
.sorted(comparing(byAge).thenComparing(byTheirName))
.collect(toList()));
```

```Sorted in ascending order by age and name:
John - 20
Jane - 21
Sara - 21
Greg - 35
```

Java函数式编程(十一)Comparator

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊