当前位置:网站首页>Comparator 排序函数式接口
Comparator 排序函数式接口
2022-06-24 12:44:00 【51CTO】
Comparator是一个函数式接口,功能主要用于自定义排序规则,至于其它功能这里不记录,只记录下如何排序:
目前常用的排序方法:
数组排序、集合排序、集合转化为流排序
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));
}
}
};
// 方式一 -> 数组工具类:Arrays.sort(T[] a, Comparator<? super T> c);
// 方式二 -> 集合的stream()方法:new ArrayList<>().stream().sorted(Comparator<? super T> comparator);
// 方式二 -> 集合工具类:
Collections. sort( usersList, new Comparator < User >() {
public int compare( User o1, User o2) {
// 倒序
return o2. getAge() - o1. getAge();
// 正序
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.
实际开发中的案例:
若依前后端分离框架中,前台excel模板下载时,后端其实是新建了一个excel,然后把数据填充进去, 内部原理:通过反射来获取要导出的实体类中的每一个字段,添加到list列表中,循环遍历list列表,把带有@Excel注解的字段和@Excel注解中的属性添加到 一个Object[]数组中去,然后把Object[] 数组添加到fields 列表中去,最后把列表转化为流,进行排序。
边栏推荐
- Experience of IOS interview strategy - App testing and launching
- Attack Science: ARP attack
- On the value foam of digital copyright works from the controversial nature of "Meng Hua Lu"
- Configuration (enable_*) parameter related to execution plan in PG
- MySQL foreign key impact
- Encapsulate the method of converting a picture file object to Base64
- Five minutes to develop your own code generator
- Who said that "programmers are useless without computers? The big brother around me disagrees! It's true
- Cohere、OpenAI、AI21联合发布部署模型的最佳实践准则
- Kubernetes集群部署
猜你喜欢
《回归故里》阅读笔记
Yolov6: the fast and accurate target detection framework is open source
从《梦华录》的争议性,谈谈数字版权作品的价值泡沫
Understanding openstack network
"Interesting" is the competitiveness of the new era
CVPR 2022 - Interpretation of selected papers of meituan technical team
线程同步的基石AbstractQueuedSynchronizer详解
openGauss内核:简单查询的执行
Opengauss kernel: simple query execution
Use terminal to activate CONDA service in pypharm (the ultimate method is definitely OK)
随机推荐
Summary of the process of restoring damaged data in MySQL database
Interesting erasure code
实现领域驱动设计 - 使用ABP框架 - 更新操作实体
Use abp Zero builds a third-party login module (I): Principles
1、贪吃蛇游戏设计
爱可可AI前沿推介(6.24)
About me, a 19 line programmer
[log service CLS] Tencent cloud log service CLS accesses CDN
[programming navigation] the practical code summarized by foreign great God, learned in 30 seconds!
我开导一个朋友的一些话以及我个人对《六祖坛经》的一点感悟
What if the WordPress website forgets its password
How can ffmpeg streaming to the server save video as a file through easydss video platform?
[2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve
Continuous testing | key to efficient testing in Devops Era
Configure Yum proxy
【数据库】期末复习(计科版)
强化学习之父Richard Sutton论文:追寻智能决策者的通用模型
Generate the NC file of 4-D air pressure and temperature, and then read the code (provide the code)
微医CodeReview工具链
“有趣” 是新时代的竞争力