当前位置:网站首页>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 .
边栏推荐
- Looking back on 2021, looking forward to 2022 | a year between CSDN and me
- Online text line fixed length fill tool
- Study notes 7
- Interview related high-frequency algorithm test site 3
- Threejs Internet of things, 3D visualization of factory
- [understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
- MacBook安装postgreSQL+postgis
- “金九银十”是找工作的最佳时期吗?那倒未必
- Ctfshow web entry code audit
- EasyCVR更改录像存储路径,不生成录像文件如何解决?
猜你喜欢
随机推荐
ClickPaaS低代码平台
UI automation test farewell to manual download of browser driver
EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
Uni app change the default component style
Network security - record web vulnerability fixes
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
我国算力规模排名全球第二:计算正向智算跨越
Plasticscm enterprise crack
Seven join join queries of MySQL
在线SQL转Excel(xls/xlsx)工具
[phantom engine UE] realize the animation production of mapping tripod deployment
provide/inject
[moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
Learning notes 8
Behavior perception system
A solution to the problem that variables cannot change dynamically when debugging in keil5
How to realize real-time audio and video chat function
Learning MVVM notes (1)
Differences among 10 addressing modes
Sequelize. JS and hasmany - belongsto vs hasmany in serialize js