当前位置:网站首页>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 .
边栏推荐
- 【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
- . Net 6 and Net core learning notes: Important issues of net core
- [ruoyi] set theme style
- 3857 Mercator coordinate system converted to 4326 (WGS84) longitude and latitude coordinates
- Leetcode problem solving -- 173 Binary search tree iterator
- resulttype和resultmap的区别和应用场景
- Analyze menu analysis
- [ruoyi] enable Mini navigation bar
- My C language learning record (blue bridge) -- on the pointer
- Recommended foreign websites for programmers to learn
猜你喜欢
MySQL advanced notes
StrError & PERROR use yyds dry inventory
Reverse repackaging of wechat applet
XSS challenges绕过防护策略进行 XSS 注入
Communication between microservices
Linear programming matlab
NR modulation 1
The next industry outlet: NFT digital collection, is it an opportunity or a foam?
【概念】Web 基础概念认知
Taobao focus map layout practice
随机推荐
MySQL Server层四个日志
OCR文字識別方法綜述
NR modulation 1
Recommended foreign websites for programmers to learn
[padding] an error is reported in the prediction after loading the model weight attributeerror: 'model' object has no attribute '_ place‘
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
【若依(ruoyi)】ztree 自定义图标(iconSkin 属性)
Crazy, thousands of netizens are exploding the company's salary
Résumé des méthodes de reconnaissance des caractères ocr
js 正则过滤和增加富文本中图片前缀
Inherit day01
The difference between sizeof and strlen in C language
Derivation of anti Park transform and anti Clarke transform formulas for motor control
Selenium share
My C language learning record (blue bridge) -- under the pointer
Era5 reanalysis data download strategy
I sorted out a classic interview question for my job hopping friends
Performance analysis of user login TPS low and CPU full
Computer graduation project asp Net fitness management system VS development SQLSERVER database web structure c programming computer web page source code project
八道超经典指针面试题(三千字详解)