文章地址: 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.
相关推荐
主要给大家介绍了关于Java 8 Comparator: 列表排序的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Java8具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
java排序Comparator和Comparable
java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子java Comparator 用法 例子
java的Comparator和Comparable
在这篇教程里,我们将要去了解下即将到来的JDK 8(译注,现在JDK 8已经发布了)中的Lambda表达式——特别是怎样使用它来编写Comparator和对集合(Collection)进行排序。 这篇文章是Baeldung上的“Java ——...
主要介绍了Java8 Comparator排序方法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了JAVA使用Comparator接口实现自定义排序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
java8流源码Java 8 集合速成课程 所有练习都可以在课堂的main方法中完成。 Lambda 表达式 例子 写下给定beerNames列表的元素: beerNames . forEach(s - > System . out . println(s)); 例子 按 aphabet 顺序对给定...
List对象集合的排序:比较器Comparator
Comator 比较器Java g
Java-中的-Comparator-和-Comparable.md
java8集合源码#Java 8 - 概述 新的功能 Lambda 表达式 方法参考 默认方法 新工具 流API 日期时间 API 选修课 Nashorn,JavaScript 引擎 Java 7 Collections.sort(names, new Comparator() { @Override public int ...
要注意的是List,Set,Queue继承了Collection接口,...这里想用一个简单的例子展示一下他们的使用,内容包括:List、Map、Set、Queue,Collections、Comparable与Comparator,排序、搜索,内部类,泛型、重写equals、hashCode
PGN-comparator:比较多个游戏,那么发生了什么变化
本文,先介绍Comparable 和Comparator两个接口,以及它们的差异;接着,通过示例,对它们的使用方法进行说明
主要介绍了Java8 Comparator源码演示及解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java中Comparator接口的用法 对集合排序的例子 Comparator
GraphQL :: SchemaComparator GraphQL::SchemaComparator... gem install graphql-schema_comparator ,以这种方式使用CLI: Commands: schema_comparator compare OLD_SCHEMA NEW_SCHEMA # Compares OLD_SCHEMA with
nba-comparator:Jagiellonian大学脚本语言课程的简单烧瓶应用程序,使用从篮球参考中获得的篮球数据
像这样的日志代码会更好: if (log.isLoggable(Level.FINE)) { ...假定你已经完成了分析,并且判断出是运行环境中Java 组件的性能需要改善。 ——参考资料:仅个人学习,未详尽测试,请自行调试!!