Java 8 比较器:如何对 List 排序
在本文中,比较我们将看到几个关于如何在Java 8中对List进行排序的器何例子。
按字母排序字符串列表
List<String> cities = Arrays.asList( "Milan",比较 "london", "San Francisco", "Tokyo", "New Delhi" ); System.out.println(cities); //[Milan, london, San Francisco, Tokyo, New Delhi] cities.sort(String.CASE_INSENSITIVE_ORDER); System.out.println(cities); //[london, Milan, New Delhi, San Francisco, Tokyo] cities.sort(Comparator.naturalOrder()); System.out.println(cities); //[Milan, New Delhi, San Francisco, Tokyo, london]London的“L”使用小写字母,是器何为了更好地突出 Comparator.naturalOrder() (返回首先排序大写字母的比较器)和 String.CASE_INSENSITIVE_ORDER(返回不区分大小写的比较器)之间的差异。
基本上,比较在Java 7中,器何我们使用Collection.sort()接受List和最后的比较Comparator ——在Java 8中,我们有新的器何 List.sort() 用于接受Comparator。
对整数列表排序
List<Integer> numbers = Arrays.asList(6,比较 2, 1, 4, 9); System.out.println(numbers); //[6, 2, 1, 4, 9] numbers.sort(Comparator.naturalOrder()); System.out.println(numbers); //[1, 2, 4, 6, 9]按字符串字段对列表排序
假设我们有一个Movie类,并且我们要“按标题title”对List排序。器何我们可以使用 Comparator.comparing() ,比较传递一个函数,器何函数提取用于排序title的比较字段——在本例中。
List<Movie> movies = Arrays.asList( new Movie("Lord of the rings"),器何 new Movie("Back to the future"), new Movie("Carlitos way"), new Movie("Pulp fiction")); movies.sort(Comparator.comparing(Movie::getTitle)); movies.forEach(System.out::println);输出:
Movie{ title=Back to the future} Movie{ title=Carlitos way} Movie{ title=Lord of the rings} Movie{ title=Pulp fiction}可能你会注意到我们没有通过任何Comparator ,但正确排序了List。比较这是因为title——提取的字段——是一个字符串,并且字符串实现了可比较的接口。亿华云如果你看看Comparator.comparing()实现,你会看到它对提取的键调用compareTo。
return (Comparator<T> & Serializable) (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));按double字段对列表排序
以类似的方式,我们可以使用 Comparator.comparingDouble()来比较double值。在示例中,我们想按最高到最低的评分来订购Movie列表。
List<Movie> movies = Arrays.asList( new Movie("Lord of the rings", 8.8), new Movie("Back to the future", 8.5), new Movie("Carlitos way", 7.9), new Movie("Pulp fiction", 8.9)); movies.sort(Comparator.comparingDouble(Movie::getRating) .reversed()); movies.forEach(System.out::println);我们在Comparator上使用reversed函数,以便反转默认的从最低到最高的自然顺序。 Comparator.comparingDouble()在内部使用Double.compare()。
如果你需要比较int或long,那么你可以分别使用comparingInt()和comparingLong()。
使用自定义比较器对列表排序
在前面的例子中,我们没有指定任何比较器,因为没有必要,但让我们看一个例子,例子中我们定义了我们自己的比较器。我们的Movie类有一个新的字段——“starred”——使用第三个构造函数参数设置。在示例中,我们要对列表进行排序,以便列表顶部为已加星标的服务器租用电影。
List<Movie> movies = Arrays.asList( new Movie("Lord of the rings", 8.8, true), new Movie("Back to the future", 8.5, false), new Movie("Carlitos way", 7.9, true), new Movie("Pulp fiction", 8.9, false)); movies.sort(new Comparator<Movie>() { @Override public int compare(Movie m1, Movie m2) { if(m1.getStarred() == m2.getStarred()){ return 0; } return m1.getStarred() ? -1 : 1; } }); movies.forEach(System.out::println);结果将是:
Movie{ starred=true, title=Lord of the rings, rating=8.8} Movie{ starred=true, title=Carlitos way, rating=7.9} Movie{ starred=false, title=Back to the future, rating=8.5} Movie{ starred=false, title=Pulp fiction, rating=8.9}我们当然可以使用lambda表达式而不是Anonymous类,如下所示:
movies.sort((m1, m2) -> { if(m1.getStarred() == m2.getStarred()){ return 0; } return m1.getStarred() ? -1 : 1; });我们也可以再次使用Comparator.comparing():
movies.sort(Comparator.comparing(Movie::getStarred, (star1, star2) -> { if(star1 == star2){ return 0; } return star1 ? -1 : 1; }));在最新的示例中,Comparator.comparing()将第一个参数作为提取用于排序的键的函数,并将Comparator作为第二个参数。Comparator 使用提取的键进行比较,star1和star2真是布尔值,分别表示m1.getStarred()和m2.getStarred()。
用比较链排序列表
在最后一个例子中,我们要在顶部加上已加星标的电影,然后按评分排序。
List<Movie> movies = Arrays.asList( new Movie("Lord of the rings", 8.8, true), new Movie("Back to the future", 8.5, false), new Movie("Carlitos way", 7.9, true), new Movie("Pulp fiction", 8.9, false)); movies.sort(Comparator.comparing(Movie::getStarred) .reversed() .thenComparing(Comparator.comparing(Movie::getRating) .reversed()) ); movies.forEach(System.out::println);输出是:
Movie{ starred=true, title=Lord of the rings, rating=8.8} Movie{ starred=true, title=Carlitos way, rating=7.9} Movie{ starred=false, title=Pulp fiction, rating=8.9} Movie{ starred=false, title=Back to the future, rating=8.5}正如你所看到的,我们首先按星标,然后按评分进行排序——两者都反转,因为我们想要最高的值和真正的第一。
译文链接:http://www.codeceo.com/article/java-8-comparator-sort-list.html
英文原文:Java 8 Comparator: How to Sort a List
翻译作者:码农网 – 小峰