当前位置:网站首页>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 .
边栏推荐
- NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
- Rust blockchain development - signature encryption and private key public key
- EasyCVR更改录像存储路径,不生成录像文件如何解决?
- A應用喚醒B應該快速方法
- Rust区块琏开发——签名加密与私钥公钥
- 【刷题】BFS题目精选
- Uni app common functions /api
- IronXL for . NET 2022.6
- Threejs realizes rain, snow, overcast, sunny, flame
- [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)
猜你喜欢
How does the applet solve the rendering layer network layer error?
[wp]bmzclub writeup of several questions
“金九银十”是找工作的最佳时期吗?那倒未必
已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘
A real day for Beijing programmers!!!!!
Threejs realizes sky box, panoramic scene, ground grass
EasyCVR更改录像存储路径,不生成录像文件如何解决?
As soon as I write the code, President Wang talks with me about the pattern all day
蛇形矩阵
Use of vscode software
随机推荐
【刷题】BFS题目精选
机器学习 --- 决策树
【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
A real day for Beijing programmers!!!!!
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
Open graph protocol
Interview summary: This is a comprehensive & detailed Android interview guide
[array]566 Reshape the matrix - simple
ClickPaaS低代码平台
NetSetMan pro (IP fast switching tool) official Chinese version v5.1.0 | computer IP switching software download
Uni app change the default component style
After the deployment of web resources, the navigator cannot obtain the solution of mediadevices instance (navigator.mediadevices is undefined)
[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)
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
Possible stack order of stack order with length n
Get to know MySQL connection query for the first time
Online sql to excel (xls/xlsx) tool
面试字节,过关斩将直接干到 3 面,结果找了个架构师来吊打我?
Resolved (sqlalchemy+pandas.read_sql) attributeerror: 'engine' object has no attribute 'execution_ options‘
Bit operation skills