当前位置:网站首页>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 .
边栏推荐
- Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
- 已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘
- 我就一写代码的,王总整天和我谈格局...
- lds链接的 顺序问题
- How to use jedis of redis
- How to realize real-time audio and video chat function
- EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
- 蛇形矩阵
- Judge whether the stack order is reasonable according to the stack order
- Is there a sudden failure on the line? How to make emergency diagnosis, troubleshooting and recovery
猜你喜欢
As soon as I write the code, President Wang talks with me about the pattern all day
Why do big companies such as Baidu and Alibaba prefer to spend 25K to recruit fresh students rather than raise wages by 5K to retain old employees?
Threejs implements labels and displays labels with custom styles
Longyuan war "epidemic" 2021 network security competition web easyjaba
【刷题】BFS题目精选
Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes
阿里云ECS使用cloudfs4oss挂载OSS
“金九银十”是找工作的最佳时期吗?那倒未必
How to realize real-time audio and video chat function
随机推荐
在线SQL转Excel(xls/xlsx)工具
Is "golden nine and silver ten" the best time to find a job? Not necessarily
EasyCVR更改录像存储路径,不生成录像文件如何解决?
A application wakes up B should be a fast method
JVM garbage collection
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
[wp]bmzclub writeup of several questions
Plasticscm enterprise crack
【刷题】BFS题目精选
快手、抖音、视频号交战内容付费
网络安全-记录web漏洞修复
Use Firefox browser to quickly pick up Web image materials
Official announcement! The third cloud native programming challenge is officially launched!
Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
provide/inject
Why can't all browsers on my computer open web pages
[brush questions] BFS topic selection
Learning MVVM notes (1)
A solution to the problem that variables cannot change dynamically when debugging in keil5
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information