当前位置:网站首页>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、贪吃蛇游戏设计
- Cohere、OpenAI、AI21联合发布部署模型的最佳实践准则
- Configure Yum proxy
- Configuration (enable_*) parameter related to execution plan in PG
- How do websites and we media tap user needs? Deeply expose the secrets behind the keywords!
- [database] final review (planning Edition)
- Implement Domain Driven Design - use ABP framework - create entities
- On the value foam of digital copyright works from the controversial nature of "Meng Hua Lu"
- Attack Science: DDoS (Part 2)
- MySQL 外键影响
猜你喜欢

Detailed explanation of abstractqueuedsynchronizer, the cornerstone of thread synchronization

Getting started with the go Cobra command line tool

Concept + formula (excluding parameter estimation)

【2022国赛模拟】摆(bigben)——行列式、杜教筛

一文讲透研发效能!您关心的问题都在

Reading notes of returning to hometown

几种常见的DoS攻击

The text to voice function is available online. You can experience the services of professional broadcasters. We sincerely invite you to try it out

Getting started with the lvgl Library - colors and images

使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
随机推荐
DTU上报的数据值无法通过腾讯云规则引擎填入腾讯云数据库中
JS和TS中常用特殊字符
Attack Science: ARP attack
WPF从零到1教程详解,适合新手上路
解析nc格式文件,GRB格式文件的依赖包edu.ucar.netcdfAll的api 学习
【数据库】期末复习(计科版)
IOMMU (VII) -vfio and mdev
使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
Who said that "programmers are useless without computers? The big brother around me disagrees! It's true
Optimization of MP4 file missing seconds caused by TS files when downloading videos from easydss video platform
一纸英雄帖,激起千层浪,横跨10国,一线大厂都派人来了!-GWEI 2022-新加坡
Design and implementation of high performance go log library zap
Parti,谷歌的自回归文生图模型
谁是鱼谁是饵?红队视角下蜜罐识别方式汇总
Getting started with the lvgl Library - colors and images
Istio practical skills: using prism to construct multi version test services
“有趣” 是新时代的竞争力
Configuration (enable_*) parameter related to execution plan in PG
What if the WordPress website forgets its password
【2022国赛模拟】摆(bigben)——行列式、杜教筛