Java 8 Comparator: How to Sort a List
文章地址: https://dzone.com/articles/java-8-comparator-how-to-sort-a-list
In this article, we’re going to see several examples on how to sort a List in Java 8.
Sort a List of Strings Alphabetically
We’ve written London with a lowercase "L" to better highlight differences between Comparator.naturalOrder(), which returns a Comparator that sorts by placing capital letters first, and String.CASE_INSENSITIVE_ORDER, which returns a case-insensitive Comparator.
Basically, in Java 7, we were using Collections.sort() that was accepting a List and, eventually, a Comparator – in Java 8 we have the new List.sort(), which accepts a Comparator.
Sort a List of Integers
Sort a List by String Field
Let’s suppose we have our Movie class and we want to sort our List by title. We can use Comparator.comparing() and pass a function that extracts the field to use for sorting – title, in this example.
The output will be:
As you’ve probably noticed, we haven’t passed a Comparator, but the List is correctly sorted. That’s because title, the extracted field, is a String, and a String implements a Comparable interface. If you peek at the Comparator.comparing() implementation, you will see that it calls compareTo
on the extracted key.
Sort a List by Double Field
In a similar way, we can use Comparator.comparingDouble() for comparing double value. In the example, we want to order our List of movies by rating, from the highest to the lowest.
We used the reversed function on the Comparator in order to invert default natural order; that is, from lowest to highest. Comparator.comparingDouble() uses Double.compare() under the hood.
If you need to compare int or long, you can use comparingInt() and comparingLong() respectively.
Sort a List With a Custom Comparator
In the previous examples, we haven’t specified any Comparator since it wasn’t necessary, but let’s see an example in which we define our own Comparator. Our Movie class has a new field – “starred” – set using the third constructor parameter. In the example, we want to sort the list so that we have starred movies at the top of the List.
The result will be:
We can, of course, use a lambda expression instead of Anonymous class, as follows:
We can also use Comparator.comparing() again:
In the last example, Comparator.comparing() takes the function to extract the key to use for sorting as the first parameter, and a Comparator as the second parameter. This Comparator uses the extracted keys for comparison; star1 and star2 are boolean and represent m1.getStarred() and m2.getStarred() respectively.
Sort a List With Chain of Comparators
In the last example, we want to have starred movie at the top and then sort by rating.
And the output is:
As you’ve seen, we first sort by starred and then by rating – both reversed because we want highest value and true first.
推荐阅读
-
Java8利用stream的distinct()方法对list集合中的对象去重和抽取属性去重
-
Java8 Comparator: 列表排序的深入讲解
-
Java8处理List的双层循环问题
-
java8 stream 由一个list转化成另一个list案例
-
List -> Map
> demo java 8StreamListMap -
java 8 Stream list to Map key 重复 value合并到Collection
-
Java对学生成绩排序——通过list.sort()对list进行排序
-
Java8-Stream流操作List去重distinct、和指定字段去重(完整实例讲解)
-
java8新特性 获取list某一列的操作
-
浅谈Java中Collections.sort对List排序的两种方法