当前位置:网站首页>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 .
边栏推荐
- Interview summary: This is a comprehensive & detailed Android interview guide
- How to use jedis of redis
- EasyCVR更改录像存储路径,不生成录像文件如何解决?
- Pyqt5 displays file names and pictures
- Rome chain analysis
- 陇原战“疫“2021网络安全大赛 Web EasyJaba
- 长度为n的入栈顺序的可能出栈顺序种数
- Threejs Internet of things, 3D visualization of farms (II)
- [understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
- Sequelize. JS and hasmany - belongsto vs hasmany in serialize js
猜你喜欢

小程序中实现文章的关注功能

Rome链分析

Looking back on 2021, looking forward to 2022 | a year between CSDN and me

已解决(sqlalchemy+pandas.read_sql)AttributeError: ‘Engine‘ object has no attribute ‘execution_options‘

在线SQL转Excel(xls/xlsx)工具

Seven join join queries of MySQL

As soon as I write the code, President Wang talks with me about the pattern all day

Threejs rendering obj+mtl model source code, 3D factory model

The new project Galaxy token just announced by coinlist is gal

MindFusion. Virtual Keyboard for WPF
随机推荐
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
Rust blockchain development - signature encryption and private key public key
kubernetes集群之调度系统
[untitled]
JVM garbage collection
lds链接的 顺序问题
Why can't all browsers on my computer open web pages
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
C language course setting: cinema ticket selling management system
About the project error reporting solution of mpaas Pb access mode adapting to 64 bit CPU architecture
This article takes you to understand the relationship between the past and present of Bi and the digital transformation of enterprises
Ffmepg usage guide
[Chongqing Guangdong education] 2408t Chinese contemporary literature reference test in autumn 2018 of the National Open University
Fuel consumption calculator
Rome chain analysis
【刷题】BFS题目精选
陇原战“疫“2021网络安全大赛 Web EasyJaba
UI automation test farewell to manual download of browser driver
Uni app common functions /api
How is the entered query SQL statement executed?