当前位置:网站首页>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 .


边栏推荐
- Encapsulate the method of converting a picture file object to Base64
- From theory to practice, decipher Alibaba's internal MySQL optimization scheme in simple terms
- Tencent released credit risk control results safely: it has helped banks lend more than 100 billion yuan
- 微医CodeReview工具链
- Detailed explanation of abstractqueuedsynchronizer, the cornerstone of thread synchronization
- Use txvideoeditor to add watermark and export video card at 99%? No successful failed callback?
- Istio practical skills: using prism to construct multi version test services
- 钉钉、飞书、企业微信:迥异的商业门道
- CVPR 2022 | interprétation de certains documents de l'équipe technique de meituan
- About the hacked database
猜你喜欢
![[database] final review (planning Edition)](/img/94/cd2df3a011c4e466df5aaa0f3bd0f2.png)
[database] final review (planning Edition)

Codereview tool chain for micro medicine

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

On the value foam of digital copyright works from the controversial nature of "Meng Hua Lu"

我真傻,招了一堆只会“谷歌”的程序员!
![[2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve](/img/ec/6c6e3d878e2a05a6e7a4ca336ae134.jpg)
[2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve

Pycharm中使用Terminal激活conda服务(终极方法,铁定可以)

钉钉、飞书、企业微信:迥异的商业门道

MySQL 外键影响

Kubernetes cluster deployment
随机推荐
Parti,谷歌的自回归文生图模型
强化学习之父Richard Sutton论文:追寻智能决策者的通用模型
几种常见的DoS攻击
Implement Domain Driven Design - use ABP framework - create entities
Use terminal to activate CONDA service in pypharm (the ultimate method is definitely OK)
How to make secruecrt more productive
SMS SMS
Interesting erasure code
线程同步的基石AbstractQueuedSynchronizer详解
Megacli online management raid installation and use steps
About me, a 19 line programmer
短信服务sms
我从根上解决了微信占用手机内存问题
How to efficiently analyze online log
Codereview tool chain for micro medicine
How does Argo family bucket make Devops easier?
Redis' contribution in the field of microservices
SCRM, a breakthrough in the new consumption era
How do websites and we media tap user needs? Deeply expose the secrets behind the keywords!
From theory to practice, decipher Alibaba's internal MySQL optimization scheme in simple terms