当前位置:网站首页>How to get the first few pieces of data of each group gracefully
How to get the first few pieces of data of each group gracefully
2022-07-05 04:12:00 【Peipei Dad】

background
Recently, when changing a function , Find out that there is a need to get the answer to each question 3 The best reply content of .
And then combine it into list. Return to the front end .
You may think this is relatively simple , Find all of them , Then one for loop , It's done .
But as an ideal , Pretending programmer , How can we do such an operation ?
Solution
One 、 Use mysql solve
This scheme is quite common online .
SQL Put on
select media_id from article a
where 3>=(select count(1) from article where `status`=1 and media_id=a.media_id and hot_num > a.hot_num)
order by a.media_id,id desc;Two 、 Use stream Of groupBy solve
Map<String, List<Article>> map = list.stream()
.collect(Collectors.groupingBy(Article::getMediaId,
Collectors.collectingAndThen(Collectors.toList(),
list2 -> list2.stream().sorted(Comparator.comparing(Article::getHdNum, Comparator.reverseOrder())).limit(3).collect(Collectors.toList()))));
summary
The data queried in the second method is a little large , It is better to use the first one .
边栏推荐
- 蛇形矩阵
- Ctfshow web entry code audit
- [phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
- Behavior perception system
- How does the applet solve the rendering layer network layer error?
- Uni app change the default component style
- 学习MVVM笔记(一)
- @The problem of cross database query invalidation caused by transactional annotation
- lds链接的 顺序问题
- Realize the attention function of the article in the applet
猜你喜欢

【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录

How to solve the problem that easycvr changes the recording storage path and does not generate recording files?

Rust区块琏开发——签名加密与私钥公钥

Rust blockchain development - signature encryption and private key public key

美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
![[phantom engine UE] realize the animation production of mapping tripod deployment](/img/89/351641c3da7e2acdbf389bc298b75e.png)
[phantom engine UE] realize the animation production of mapping tripod deployment
![[array]566 Reshape the matrix - simple](/img/3c/593156f5bde67bd56828106d7bed3c.png)
[array]566 Reshape the matrix - simple

Learning notes 8

北京程序员的真实一天!!!!!

NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
随机推荐
Learning MVVM notes (1)
美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
open graph协议
What is test development? Why do so many companies hire test developers now?
Network security - record web vulnerability fixes
MindFusion. Virtual Keyboard for WPF
[phantom engine UE] realize the animation production of mapping tripod deployment
Official announcement! The third cloud native programming challenge is officially launched!
error Couldn‘t find a package. JSON file in "your path“
【看完就懂系列】一文6000字教你从0到1实现接口自动化
UI自動化測試從此告別手動下載瀏覽器驅動
Longyuan war "epidemic" 2021 network security competition web easyjaba
About the recent experience of writing questions
Seven join join queries of MySQL
[PHP features - variable coverage] improper use, improper configuration and code logic vulnerability of the function
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes
长度为n的入栈顺序的可能出栈顺序种数
[wp]bmzclub writeup of several questions
Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery