着重基础之—Java 8 Comparator: How to Sort a List (List排序)
2017-11-22 17:50
477 查看
着重基础之—Java 8 Comparator: How to Sort a List (List排序)
首先申明,这篇博客的内容不是我自己的知识,我是从国外网站搬来的,原因有二:1是因为大天朝对网络的封锁,想用google搜点技术知识,得先FQ。2.百度上的知识点你如果仔细琢磨的话会发现,所有的搜到的知识分俩类,一类是安装教程,一类是...。
Java List排序是我们日常工作中用的比较频繁的,下面内容将介绍Java 8 Comparator 的用法:
1.字符串List 按字母顺序排列
* 这里要注意 london 小写'l'在排序过程中的细节。
2.整型数组排序
3.根据字符串字段排序
输出将会是:
4.根据Double字段排序
5.自定义排序
输出将会是:
当然,我们也可以用lambda表达式代替匿名类,如下:
6.链式表达式排序
输出结果如下:
好了,就转载到这里。
首先申明,这篇博客的内容不是我自己的知识,我是从国外网站搬来的,原因有二:1是因为大天朝对网络的封锁,想用google搜点技术知识,得先FQ。2.百度上的知识点你如果仔细琢磨的话会发现,所有的搜到的知识分俩类,一类是安装教程,一类是...。
Java List排序是我们日常工作中用的比较频繁的,下面内容将介绍Java 8 Comparator 的用法:
1.字符串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'在排序过程中的细节。
2.整型数组排序
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]
3.根据字符串字段排序
List<Movie> movies = Arrays.asList( new Movie("Lord of the rings"), new Movie("Back to the future"), new Movie("Carlito's way"), new Movie("Pulp fiction")); movies.sort(Comparator.comparing(Movie::getTitle)); movies.forEach(System.out::println);
输出将会是:
Movie{title='Back to the future'} Movie{title='Carlito's way'} Movie{title='Lord of the rings'} Movie{title='Pulp fiction'}
4.根据Double字段排序
List<Movie> movies = Arrays.asList( new Movie("Lord of the rings", 8.8), new Movie("Back to the future", 8.5), new Movie("Carlito's way", 7.9), new Movie("Pulp fiction", 8.9)); movies.sort(Comparator.comparingDouble(Movie::getRating) .reversed()); movies.forEach(System.out::println);
5.自定义排序
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("Carlito's 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='Carlito's 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表达式代替匿名类,如下:
movies.sort((m1, m2) -> { if(m1.getStarred() == m2.getStarred()){ return 0; } return m1.getStarred() ? -1 : 1; });
6.链式表达式排序
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("Carlito's 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='Carlito's way', rating=7.9} Movie{starred=false, title='Pulp fiction', rating=8.9} Movie{starred=false, title='Back to the future', rating=8.5}
好了,就转载到这里。
相关文章推荐
- How To Sort Objects With Java Interface Comparable and Comparator
- 【Java】如何给List集合排序Collections.sort(list,new Comparator<Entity>)
- Java利用Comparator与Collections.sort对List排序
- Python: How to Sort a List
- java List 排序 Collections.sort() 对 List 排序
- java List 排序 Collections.sort() 对 List 排序
- Java list排序---Collections.sort()
- 日文,中文,韩文,利用Arrays.sort()对非英文排序,并与原值比较,返回(-1,0,1), 实现 Comparator接口, list 排序
- java数组排序Arrays.sort,以及Comparator接口的用法
- java List 排序 Collections.sort() 对 List 排序
- java.util.Collections.sort(List list)与Comparable,Comparator 接口
- java List 排序 Collections.sort() 对 List 排序
- sort之选择排序(java基础)
- [基础回顾-JAVA类集框架篇] 之 集合排序Comparable与Comparator(上)
- java List 排序 Collections.sort() 对 List 排序
- java List 排序 Collections.sort
- [基础回顾-JAVA类集框架篇] 之 集合排序Comparable与Comparator(下)
- java数组排序Arrays.sort,以及结合Comparator接口的用法
- java List Collections.sort 排序 支持 对象属性 普通数据类型
- java List 排序 Collections.sort() 对 List 排序