当前位置:网站首页>Comparator sort functional interface
Comparator sort functional interface
2022-06-24 13:06:00 【51CTO】
Comparator Is a functional interface , This function is mainly used to customize sorting rules , Other functions are not recorded here , Just record how you sort :
The current common sorting methods :
Array sorting 、 Collection sorting 、 Convert collection to stream sort
public class sortTest {
public static void main( String[] args) {
List < User > usersList = new ArrayList <>(){
{
for( int i = 0; i < 3; i ++) {
add( new User( "lxc" + i, 20 + i));
}
}
};
// Mode one -> Array utility class :Arrays.sort(T[] a, Comparator<? super T> c);
// Mode two -> A collection of stream() Method :new ArrayList<>().stream().sorted(Comparator<? super T> comparator);
// Mode two -> Collection tool class :
Collections. sort( usersList, new Comparator < User >() {
public int compare( User o1, User o2) {
// In reverse order
return o2. getAge() - o1. getAge();
// positive sequence
return o1. getAge() - o2. getAge();
}
});
System. out. println( JSON. toJSONString( usersList, SerializerFeature. PrettyFormat));
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
Cases in actual development :
If the front and back ends are separated from the frame , The front desk excel When downloading template , The backend is actually a new one excel, Then fill in the data , internals : Get every field in the entity class to be exported through reflection , Add to list In the list , Loop traversal list list , The with @Excel Annotated fields and @Excel Attributes in the annotation are added to One Object[] Go to... In the array , And then put Object[] Array added to fields Go to... In the list , Finally, convert the list into a stream , Sort .
边栏推荐
- 1. Snake game design
- It's settled! Bank retail credit risk control just does it!
- Generate the NC file of 4-D air pressure and temperature, and then read the code (provide the code)
- WPF from zero to 1 tutorial details, suitable for novices on the road
- Boss direct employment IPO: both the end and the beginning
- DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中
- 简述聚类分析
- Yolov6: the fast and accurate target detection framework is open source
- 手把手教你用AirtestIDE无线连接手机!
- Ghost, a synonym for blog system
猜你喜欢
MySQL foreign key impact
go Cobra命令行工具入门
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
钉钉、飞书、企业微信:迥异的商业门道
mLife Forum | 微生物组和数据挖掘
MySQL 外键影响
Detailed explanation of abstractqueuedsynchronizer, the cornerstone of thread synchronization
CVPR 2022 | 美团技术团队精选论文解读
Internet of things? Come and see Arduino on the cloud
Reading notes of returning to hometown
随机推荐
Redis' contribution in the field of microservices
mLife Forum | 微生物组和数据挖掘
Essential key steps in the construction of e-commerce live broadcast source code
[day ui] affix component learning
Reading notes of returning to hometown
YOLOv6:又快又准的目标检测框架开源啦
About the hacked database
SMS SMS
Use terminal to activate CONDA service in pypharm (the ultimate method is definitely OK)
[live broadcast of celebrities] elastic observability workshop
华为AppLinking中统一链接的创建和使用
go Cobra命令行工具入门
Open source monitoring system Prometheus
使用 Abp.Zero 搭建第三方登录模块(一):原理篇
what the fuck! I'm flattered. He actually wrote down the answers to the redis interview questions that big companies often ask!
I enlighten a friend and my personal understanding of the six patriarchs' Tan Jing
How to do research on plant endophytes? Special topic on Microbiology
Who said that "programmers are useless without computers? The big brother around me disagrees! It's true
Smart Policing: how to use video intelligent analysis technology to help urban policing visual comprehensive supervision and command system
CVPR 2022 - Interpretation of selected papers of meituan technical team