转载-lambda sort
原文:https://blog.csdn.net/qq_27127145/article/details/83930498
版权声明:本文为博主原创文章,转载请附上博文链接!
import com.google.common.collect.Lists;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class ComparatorTest {
@Test
public void test1(){
/**
* Collections.sort()使用
*/
//被排序的集合
List<User> userList = Lists.newArrayList(new User("Jack",11),new User("Jack",10));
//1. Java8之前,使用匿名内部类的基本排序
Collections.sort(userList, new Comparator<User>() {
@Override
public int compare(User user1, User user2) {
return user1.getAge().compareTo(user2.getAge());
}
});
//2. Java8,使用Lambda表达式的基本排序
Collections.sort(userList,
(User user1, User user2) ->user1.getAge().compareTo(user2.getAge()));
//userList.sort((User user1, User user2) -> user1.getAge().compareTo(user2.getAge()));
//3. Java8,Lambda表达式可以简化,省略定义类型User
userList.sort((user1, user2) -> user1.getAge().compareTo(user2.getAge()));
//4. Java8,Lambda表达式,多条件排序
userList.sort((user1, user2) -> {
if (user1.getName().equals(user2.getName())) {
return user1.getAge() - user2.getAge();
} else {
return user1.getName().compareTo(user2.getName());
}
});
//5. Java8,多条件组合排序
userList.sort(Comparator.comparing(User::getName).thenComparing(User::getAge));
//6. Java8,提取Comparator进行排序
Collections.sort(userList, Comparator.comparing(User::getName));
//7. Java8,自定义静态的比较方法来排序(静态方法必须写在被比较的类(这里是User类)中)
userList.sort(User::compareByAgeThenName);
//8. Java8,反转排序
Comparator<User> comparator = (user1, user2) -> user1.getName().compareTo(user2.getName());
userList.sort(comparator);//先按name排序
userList.sort(comparator.reversed());//反转排序
Assert.assertEquals(userList.get(0),new User("Jack",10));
/**
* Arrays.sort()使用
*/
//被排序的字符串数组
String[] months = {"January","February","March","April","May","June","July","August","September","October","December"};
//按字符串长度排序
//1.
Arrays.sort(months, (a, b) -> Integer.signum(a.length() - b.length()));
//2.
Arrays.sort(months, Comparator.comparingInt(String::length));
//3.
Arrays.sort(months, (a, b) -> a.length() - b.length());
//4.
Arrays.sort(months,
(String a, String b) -> { return Integer.signum(a.length() - b.length()); }
);
System.out.println(Arrays.toString(months));
}
}
作者:panrusheng
链接:https://www.jianshu.com/p/914229ecf867
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
转载-lambda sort的更多相关文章
- python3学习之lambda+sort
>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')] >>> pairs.sort(key ...
- 转载 shell sort
http://blog.sina.com.cn/s/blog_6d09b5750100x6zg.html 首先是shell排序实现多列排序,这里添加竖线以作分割,如下文件test: a|gggg|4| ...
- python基础之lambda,sort,filter,map,递归函数的运用
内容梗概:1. lamda 匿名函数2. sorted()3. filter()4. map()5. 递归函数 1.lambda 形式: lambda 参数:返回值 f = lambda x,y: x ...
- sort对二维字符数组排序
转载:sort对二维字符数组排序
- python-Web-数据库-Redis
概述: >>>安装: >>>数据类型: string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合) &g ...
- hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏
partial sort. first use std::nth_element to find pivot, then use std::stable_partition with the pivo ...
- 【转载】C#中自定义Sort的排序规则IComparable接口
C#中的List集合在排序的时候,如果不使用Lambda表达式进行排序的话,一般调用Sort()方法进行排序,如果希望Sort()方法排序后的结果跟我们预想的效果一致或者按照我们自定义的规则排序,则需 ...
- 转载java 8 为什么引入 lambda
转载:https://www.cnblogs.com/keeya/p/11404631.html 在Java8出现之前,如果你想传递一段代码到另一个方法里是很不方便的.你几乎不可能将代码块到处传递,因 ...
- 【转载】C++ function、bind和lambda表达式
本篇随笔为转载,原贴地址:C++ function.bind和lambda表达式. 本文是C++0x系列的第四篇,主要是内容是C++0x中新增的lambda表达式, function对象和bind机制 ...
随机推荐
- selenium Java中常见等待的几种形式
前言 在自动化测试中,我们经常会碰到编写脚本过程中操作某个元素的时候, 需要等待页面加载完成后,才能对元素操作,否则会报错,提示页面元素不存在异常,我们需要等待元素加载完成后,才能继续操作,而Sele ...
- 【Intellij IDEA】设置 jdk 版本
File -> Project Structure... -> Project,如图所示:
- ubuntu 13.10 eclipse 菜单栏不可用的问题
最近手贱,从官网上下载了最新的版eclipse,把一原来的3.8版本替换了,然后就发现eclipse菜单栏废了,吓我一跳,以为Ubuntu又初问题,又重新弄了一次eclipse(我直接用的压缩包),上 ...
- AUTOSAR学习之RTE - 基本概念
1.什么是RTE? The Run-Time Environment (RTE) is at the heart of the AUTOSAR ECU architecture. The RTE is ...
- RocketMQ中Broker的HA策略源码分析
Broker的HA策略分为两部分①同步元数据②同步消息数据 同步元数据 在Slave启动时,会启动一个定时任务用来从master同步元数据 if (role == BrokerRole.SLAVE) ...
- 【POJ - 2139】Six Degrees of Cowvin Bacon (Floyd算法求最短路)
Six Degrees of Cowvin Bacon Descriptions 数学课上,WNJXYK忽然发现人缘也是可以被量化的,我们用一个人到其他所有人的平均距离来量化计算. 在这里定义人与人的 ...
- 基于.NET Core开发的个人博客发布至CentOS小计
早些时候,使用 .NET Framework 开发网站,只能部署在 Windows 服务器上面,近两年 .NET Core 如火如荼,乘此机会赶紧上车,最近将自己利用 .NET Core 开发的个人博 ...
- Mybatis案例超详解(上)
Mybatis案例超详解(上) 前言: 本来是想像之前一样继续跟新Mybatis,但由于种种原因,迟迟没有更新,快开学了,学了一个暑假,博客也更新了不少,我觉得我得缓缓,先整合一些案例练练,等我再成熟 ...
- laya2d 与 cad 之间的坐标转换
坐标系基本概念 直角坐标系可分为左手坐标系与右手坐标系,cad 中用到的是右手坐标系, Laya2D 中用到的是左手坐标系, Laya3D 中使用右手坐标系. 那么如何判断二维直角坐标系是左手还是右手 ...
- linux command line learn - get the absolute path of a file
get the absolute path of a file in linux readlink -f filenme [heshuai@login01 3_Variation_calling]$ ...