当前位置:网站首页>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 .
边栏推荐
- mysql的七种join连接查询
- What is the reason why the webrtc protocol video cannot be played on the easycvr platform?
- IronXL for . NET 2022.6
- Use object composition in preference to class inheritance
- Un réveil de l'application B devrait être rapide
- Differences among 10 addressing modes
- Three level linkage demo of uniapp uview u-picker components
- 机器学习 --- 决策树
- Soul 3: what is interface testing, how to play interface testing, and how to play interface automation testing?
- Rome链分析
猜你喜欢

Use of vscode software

Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes

laravel8 导出Excle文件

NEW:Devart dotConnect ADO. NET

C语言课设:影院售票管理系统

What is test development? Why do so many companies hire test developers now?

Realize the attention function of the article in the applet

Rome链分析

【UNIAPP】系统热更新实现思路

Kwai, Tiktok, video number, battle content payment
随机推荐
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
Ffmepg usage guide
10种寻址方式之间的区别
As soon as I write the code, President Wang talks with me about the pattern all day
小程序中实现文章的关注功能
DFS and BFS concepts of trees and graphs
北京程序员的真实一天!!!!!
Use object composition in preference to class inheritance
Learning notes 8
【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
25K 入职腾讯的那天,我特么哭了
Interview related high-frequency algorithm test site 3
Threejs Internet of things, 3D visualization of factory
Three level linkage demo of uniapp uview u-picker components
Test d'automatisation de l'interface utilisateur télécharger manuellement le pilote du navigateur à partir de maintenant
BDF application - topology sequence
[untitled]
How is the entered query SQL statement executed?
Mixed compilation of C and CC
Pyqt pyside custom telescopic menu bar sharing (including tutorial)