当前位置:网站首页>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 列表中去,最后把列表转化为流,进行排序。


边栏推荐
- 实现领域驱动设计 - 使用ABP框架 - 更新操作实体
- 实现领域驱动设计 - 使用ABP框架 - 创建实体
- “我这个白痴,招到了一堆只会“谷歌”的程序员!”
- Perhaps the greatest romance of programmers is to commemorate their dead mother with a software
- Who said that "programmers are useless without computers? The big brother around me disagrees! It's true
- 几种常见的DoS攻击
- Design and implementation of high performance go log library zap
- Engage in audio and video development? Several things I have to say about SRT live broadcast protocol
- The text to voice function is available online. You can experience the services of professional broadcasters. We sincerely invite you to try it out
- openGauss内核:简单查询的执行
猜你喜欢

华为AppLinking中统一链接的创建和使用

Use abp Zero builds a third-party login module (I): Principles

【数据库】期末复习(计科版)

Who said that "programmers are useless without computers? The big brother around me disagrees! It's true

Creation and use of unified links in Huawei applinking

Getting started with the go Cobra command line tool

CVPR 2022 | interprétation de certains documents de l'équipe technique de meituan

Detailed explanation of abstractqueuedsynchronizer, the cornerstone of thread synchronization

【概率论期末抱佛脚】概念+公式(不含参数估计)

CVPR 2022 | 美團技術團隊精選論文解讀
随机推荐
Troubleshooting and optimization of files that cannot be globally searched by ordinary users in easydss video platform customization project
Concept + formula (excluding parameter estimation)
Creation and use of unified links in Huawei applinking
Configuration (enable_*) parameter related to execution plan in PG
Megacli online management raid installation and use steps
Nifi from introduction to practice (nanny level tutorial) - environment
Optimization of MP4 file missing seconds caused by TS files when downloading videos from easydss video platform
go Cobra命令行工具入门
【2022国赛模拟】摆(bigben)——行列式、杜教筛
Redis' contribution in the field of microservices
Brief introduction to cluster analysis
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
Kubernetes practical skill: entering container netns
[log service CLS] Tencent cloud log service CLS accesses CDN
【概率论期末抱佛脚】概念+公式(不含参数估计)
"Interesting" is the competitiveness of the new era
Reading notes of returning to hometown
Babbitt | metauniverse daily must read: 618 scores have been announced. How much contribution has the digital collection made behind this satisfactory answer