当前位置:网站首页>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 .
边栏推荐
- 【看完就懂系列】一文6000字教你从0到1实现接口自动化
- Plasticscm enterprise crack
- Study notes 7
- MacBook installation postgresql+postgis
- [phantom engine UE] package error appears! Solutions to findpin errors
- Special Edition: spreadjs v15.1 vs spreadjs v15.0
- EasyCVR更改录像存储路径,不生成录像文件如何解决?
- ActiveReportsJS 3.1 VS ActiveReportsJS 3.0
- Possible stack order of stack order with length n
- Laravel8 export excel file
猜你喜欢

网络安全-记录web漏洞修复
![[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

【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)

Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
![[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)](/img/eb/a93630aff7545c6c3b71dcc9f5aa61.png)
[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)

Kwai, Tiktok, video number, battle content payment

Threejs Internet of things, 3D visualization of factory

Rust blockchain development - signature encryption and private key public key

Rome链分析

Rust区块琏开发——签名加密与私钥公钥
随机推荐
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
网络安全-记录web漏洞修复
MacBook安装postgreSQL+postgis
【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
Threejs realizes the drawing of the earth, geographical location annotation, longitude and latitude conversion of world coordinates threejs coordinates
3. Package the bottom navigation tabbar
Kwai, Tiktok, video number, battle content payment
Rust blockchain development - signature encryption and private key public key
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?
【虚幻引擎UE】实现测绘三脚架展开动画制作
MindFusion. Virtual Keyboard for WPF
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
mysql的七种join连接查询
MacBook installation postgresql+postgis
为什么百度、阿里这些大厂宁愿花25K招聘应届生,也不愿涨薪5K留住老员工?
About the recent experience of writing questions
Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes
Get to know MySQL connection query for the first time
Online sql to excel (xls/xlsx) tool
CTF stegano practice stegano 9