当前位置:网站首页>Map sorts according to the key value (ascending plus descending)
Map sorts according to the key value (ascending plus descending)
2022-07-06 03:15:00 【Zyyyyu_】
Map according to Key Value to sort
If this article helps you , I hope I can praise the blogger , thank !!
When writing a function today , You need to group by date , So when I look it up from the database, I use order by create_time desc Sort in descending order , At first, the data is sorted normally , But when I use stream() After grouping , It is found that there is a problem in data sorting 
Group by date first , Date as Map Of key, The data list corresponding to the date is used as value For storage , But it can be seen from the figure that this date is not in descending order , This will cause the front end to obtain these data and cannot output them in the correct order .
So we have to deal with this listMap Sort again .
List<EarnDetailParam> list=cereShopOrderDAO.getEarnDetails(param);
list.stream().forEach(l->l.setDate(l.getCreateTime().split(" ")[0]));
Map<String,List<EarnDetailParam>> listMap = list.stream().collect(Collectors.groupingBy(EarnDetailParam::getDate));
Map<String, List<EarnDetailParam>> result = new LinkedHashMap<>();
listMap.entrySet().stream().sorted(Collections.reverseOrder(Map.Entry.comparingByKey())).forEachOrdered(x -> result.put(x.getKey(), x.getValue()));
First, we get data from the database according to the first statement , because create_time With time , So when grouping, you need a new variable to save create_time Date after cutting list.stream().collect(Collectors.groupingBy(EarnDetailParam::getDate)) This statement uses the just obtained date Group operation , Then you will get the above figure .
Because the order is not the descending order we want , So we need to do the next step , Define a LinkedHashMap For storage , And then according to Map Of Key Sort , Because we want to operate in descending order , So we need to .sorted(Collections.reverseOrder(Map.Entry.comparingByKey())) Use reverseOrder In reverse order , Then assign the key value pair to result
.sorted(Map.Entry.comparingByKey())It's in ascending order.sorted(Collections.reverseOrder(Map.Entry.comparingByKey()))It's in reverse order

At this point, we will get the descending grouping we need , We have achieved the desired effect .
边栏推荐
- 1003 emergency (25 points), "DIJ deformation"
- 电机控制反Park变换和反Clarke变换公式推导
- Zhang Lijun: penetrating uncertainty depends on four "invariants"
- Explore pointers and pointer types in depth
- Self made CA certificate and SSL certificate using OpenSSL
- Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
- [pointer training - eight questions]
- 多态day02
- Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition
- Inherit day01
猜你喜欢
How to do function test well

2022工作中遇到的问题四

【概念】Web 基础概念认知

js 正则过滤和增加富文本中图片前缀

华为、H3C、思科命令对比,思维导图形式从基础、交换、路由三大方向介绍【转自微信公众号网络技术联盟站】

Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition

【指针训练——八道题】

The next industry outlet: NFT digital collection, is it an opportunity or a foam?

银行核心业务系统性能测试方法

Overview of OCR character recognition methods
随机推荐
SD卡报错“error -110 whilst initialising SD card
指针笔试题~走近大厂
#PAT#day10
Custom attribute access__ getattribute__/ Settings__ setattr__/ Delete__ delattr__ method
My C language learning record (blue bridge) -- under the pointer
手写数据库客户端
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
[kubernetes series] learn the exposed application of kubernetes service security
Rust language -- iterators and closures
Redis cache breakdown, cache penetration, cache avalanche
resulttype和resultmap的区别和应用场景
Handwriting database client
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
Linear regression and logistic regression
mysqldump数据备份
Add one to non negative integers in the array
Idea push rejected solution
Era5 reanalysis data download strategy
2022工作中遇到的问题四
原型图设计