当前位置:网站首页>如何优雅的获取每个分组的前几条数据
如何优雅的获取每个分组的前几条数据
2022-07-05 04:06:00 【沛沛老爹】

背景
最近在改一个功能时,发现有个需要获取每个问题回答的前3条的最优回复内容。
然后组合成list。返回给到前端。
大家可能觉得这个比较简单,找出所有的来,然后一个for循环,解决完事。
但是作为有理想的,装逼的程序员,我们怎么能做这样的操作?
解决方案
一、使用mysql解决
这个方案网上比较常见。
SQL贴上
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;二、使用stream的groupBy解决
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()))));
总结
第二种方式中查询的数据有点大,建议还是用第一种好点。
边栏推荐
- Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
- EasyCVR更改录像存储路径,不生成录像文件如何解决?
- Ctfshow web entry code audit
- [array]566 Reshape the matrix - simple
- 快手、抖音、视频号交战内容付费
- Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
- Differences among 10 addressing modes
- Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route
- [wp]bmzclub writeup of several questions
- Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes
猜你喜欢

An elegant program for Euclid‘s algorithm

The new project Galaxy token just announced by coinlist is gal

UI自動化測試從此告別手動下載瀏覽器驅動

What is the reason why the webrtc protocol video cannot be played on the easycvr platform?

This article takes you to understand the relationship between the past and present of Bi and the digital transformation of enterprises

UI自动化测试从此告别手动下载浏览器驱动

Special Edition: spreadjs v15.1 vs spreadjs v15.0

How is the entered query SQL statement executed?

Enterprise level: spire Office for . NET:Platinum|7.7. x

Clickhouse synchronization MySQL (based on materialization engine)
随机推荐
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
Threejs clicks the scene object to obtain object information, and threejs uses raycaster to pick up object information
EasyCVR更改录像存储路径,不生成录像文件如何解决?
【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
This article takes you to understand the relationship between the past and present of Bi and the digital transformation of enterprises
Realize the attention function of the article in the applet
我就一写代码的,王总整天和我谈格局...
An elegant program for Euclid‘s algorithm
Containerd series - detailed explanation of plugins
IronXL for .NET 2022.6
Uni app change the default component style
Uni app common functions /api
Learning notes 8
Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
A應用喚醒B應該快速方法
输入的查询SQL语句,是如何执行的?
Summary of scene design
Clickhouse synchronization MySQL (based on materialization engine)
Online text line fixed length fill tool