当前位置:网站首页>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 .
边栏推荐
- tcpdump: no suitable device found
- [ruoyi] ztree custom icon (iconskin attribute)
- What is the investment value of iFLYTEK, which does not make money?
- Deep parsing pointer and array written test questions
- Era5 reanalysis data download strategy
- Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
- Arabellacpc 2019 (supplementary question)
- Classic interview question [gem pirate]
- OCR文字识别方法综述
- Function knowledge points
猜你喜欢
MPLS experiment
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]
2022工作中遇到的问题四
I sorted out a classic interview question for my job hopping friends
Safety science to | travel, you must read a guide
Era5 reanalysis data download strategy
NR modulation 1
1.16 - 校验码
Apt installation ZABBIX
C # create self host webservice
随机推荐
【若依(ruoyi)】ztree 自定义图标(iconSkin 属性)
八道超经典指针面试题(三千字详解)
Game theory matlab
ASU & OSU | model based regularized off-line meta reinforcement learning
Distributed service framework dobbo
Buuctf question brushing notes - [geek challenge 2019] easysql 1
StrError & PERROR use yyds dry inventory
Shell 传递参数
1003 emergency (25 points), "DIJ deformation"
原型图设计
jsscript
[Li Kou] the second set of the 280 Li Kou weekly match
出现Permission denied的解决办法(750权限谨慎使用)
Erreur de la carte SD "erreur - 110 whilst initialisation de la carte SD
How to do function test well
MySQL advanced notes
svg拖动点裁剪图片js特效
【 kubernets series】 a Literature Study on the Safe exposure Applications of kubernets Service
SAP ALV颜色代码对应颜色(整理)
Zhang Lijun: penetrating uncertainty depends on four "invariants"