几种list排序方法
package com.lgx.jdk8.part02; import java.util.*;
import java.util.stream.Collectors; /**
* Comparator比较器的使用
*/
public class TestComparator {
public static void main(String[] args) {
List<String> list = Arrays.asList("nice", "to", "meet", "you"); //升序排序,流的写法
//list.stream().sorted(Comparator.comparing(str -> str)).collect(Collectors.toList()).forEach(System.out::println); //升序排序,默认写法
/*Collections.sort(list);
System.out.println("list = " + list);*/ //自定义排序器,默认写法
/*Collections.sort(list, (item1, item2) -> item1.length() - item2.length()); //升序
Collections.sort(list, (item1, item2) -> item2.length() - item1.length()); //降序
System.out.println("list = " + list);*/ //自定义排序器,Comparator写法
/*Collections.sort(list, Comparator.comparingInt(str -> str.length())); //升序
Collections.sort(list, Comparator.comparingInt(str -> str.length()).reversed()); //降序,会报错,因为编译器在这里无法推断str的类型为String,而是推断出事一个Objcet
Collections.sort(list, Comparator.comparingInt((String str) -> str.length()).reversed());*/ //降序,显示的指定一个类型 //自定义排序器,Comparator写法2
/*Collections.sort(list, Comparator.comparingInt(String::length)); //升序
Collections.sort(list, Comparator.comparingInt(String::length).reversed()); //降序
System.out.println("list = " + list);*/ //直接调用list的排序方法,Collections.sort()本质还是调用list.sort方法
/*list.sort(Comparator.comparingInt(String::length)); //升序
list.sort(Comparator.comparingInt(String::length).reversed()); //降序*/ /**两层排序:先按照长度排序,再按照字符串顺序**/
//Collections.sort(list, Comparator.comparingInt(String::length).thenComparing(String.CASE_INSENSITIVE_ORDER)); //不区分大小写的排序
//Collections.sort(list, Comparator.comparingInt(String::length).thenComparing(String::compareTo));
//Collections.sort(list, Comparator.comparingInt(String::length).thenComparing((item1, item2) -> item1.toLowerCase().compareTo(item2.toLowerCase())));
//Collections.sort(list, Comparator.comparingInt(String::length).thenComparing(Comparator.comparing(String::toLowerCase)));
//Collections.sort(list, Comparator.comparingInt(String::length).thenComparing(Comparator.comparing(String::toLowerCase, Comparator.reverseOrder())));
Collections.sort(list, Comparator.comparingInt(String::length).thenComparing(Comparator.comparing(String::toLowerCase, Comparator.reverseOrder())).
thenComparing(Comparator.reverseOrder()));//和上一个结果是一样的,因为已经排好序了,最后一个就不起作用了
System.out.println("list = " + list); }
}
几种list排序方法的更多相关文章
- 转:Java实现几种常见排序方法
日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序,甚至还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等. 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一 ...
- Java实现几种常见排序方法
日常操作中常见的排序方法有:冒泡排序.快速排序.选择排序.插入排序.希尔排序,甚至还有基数排序.鸡尾酒排序.桶排序.鸽巢排序.归并排序等. 以下常见算法的定义 1. 插入排序:插入排序基本操作就是将一 ...
- 笔试算法题(53):四种基本排序方法的性能特征(Selection,Insertion,Bubble,Shell)
四种基本算法概述: 基本排序:选择,插入,冒泡,希尔.上述算法适用于小规模文件和特殊文件的排序,并不适合大规模随机排序的文件.前三种算法的执行时间与N2成正比,希尔算法的执行时间与N3/2(或更快)成 ...
- Python的几种lambda排序方法
1.对单个变量进行排序 #lst = [[5,8],[5,3],[3,1]] lst.sort(key = lambda x : x[1]) #lst = [[3,1],[5,8],[5,3]] 以元 ...
- C# - List.Sort()自定义排序方法
本文通过示例介绍了C#中典型容器List.Sort()的自定义排序方法,进而引出了C#中自定义排序的核心接口及方法 项目地址:自定义Sort方法 - SouthBegonia's Github Lis ...
- php语言实现的7种基本的排序方法
今天总结了一下常用的7种排序方法,并用php语言实现. 直接插入排序 /* * 直接插入排序,插入排序的思想是:当前插入位置之前的元素有序, * 若插入当前位置的元素比有序元素最后一个元素大,则什么也 ...
- c#实现几种排序方法
插入排序 1.简介 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法.它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入.插入排序 ...
- JAVA中运用数组的四种排序方法
JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法. 快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现. 冒泡法是运用遍历数组进 ...
- java数组中的三种排序方法中的冒泡排序方法
我记得我大学学java的时候,怎么就是搞不明白这三种排序方法,也一直不会,现在我有发过来学习下这三种方法并记录下来. 首先说说冒泡排序方法:冒泡排序方法就是把数组中的每一个元素进行比较,如果第i个元素 ...
随机推荐
- github优缺点
以前bitbucket沒有支援git github可以直接在網站上瀏覽push的圖片 github 可以針對code行數直接留言與回覆 github Markdown支援很好 github 的issu ...
- [转载] Activiti Tenant Id 字段释疑
TENANT_ID_ : 这个字段表示租户ID.可以应对多租户的设计. 转载自: http://www.cnblogs.com/yg_zhang/p/4201288.html http://www. ...
- CentOS75 安装Oracle18c
1. 参考地址 https://blog.csdn.net/u010257584/article/details/50902472https://www.cnblogs.com/kerrycode/a ...
- ES6学习笔记(三):与迭代相关的新东东
Symbol 概念 Symbol:一种新的原始数据类型,表示独一无二的值. 注意:Symbol函数的参数只是表示对当前Symbol值的描述,因此相同参数的Symbol函数的返回值是不相等的. // 没 ...
- 利用git从github上拉取项目
(一)准备工作 (1)安装git,下载地址是git官网:https://git-scm.com/点击打开链接 (二)拉取项目的步骤 1.新建文件夹,最好为英文名project 2.进入文件夹,空白处右 ...
- MT【81】含参数三次函数因式分解
解答: 评:这题实质上是对关于$x$的三次函数进行了一个因式分解.这种把$a$看成主元的技巧是初中处理高次的因式分解的常用技巧.如果用三次求导去做计算量比较大,要计算极值.
- Spring Boot -Shiro配置多Realm
核心类简介 xxxToken:用户凭证 xxxFilter:生产token,设置登录成功,登录失败处理方法,判断是否登录连接等 xxxRealm:依据配置的支持Token来认证用户信息,授权用户权限 ...
- 如何整合Office Web Apps至自己开发的系统(一)
在前面我的一篇博客中 Office Web Apps安装部署(一),有一张介绍Office Web Apps与其他系统的关系图, 从上述图中,可知实际上Office Web Apps也是可以接入自 ...
- 【bzoj1758】 Wc2010—重建计划
http://www.lydsy.com/JudgeOnline/problem.php?id=1758 (题目链接) 题意 给出一棵树,每条边有边权,问选出一条长度为$[L,U]$的路径,使得路径上 ...
- 函数、可变参数、keyword-only参数、实参解构
函数的数学定义:y=f(x) ,y是x的函数,x是自变量.y=f(x0, x1, ..., xn) python中的函数: 由函数名称.参数列表.和若干语句组成的语句块构成,完成一定的功能,是组织代码 ...