当前位置:网站首页>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 .
边栏推荐
猜你喜欢
[unity3d] GUI control
MySQL advanced notes
C # create self host webservice
Sign SSL certificate as Ca
适合程序员学习的国外网站推荐
Recommended foreign websites for programmers to learn
[ruoyi] enable Mini navigation bar
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
Overview of OCR character recognition methods
Who is the winner of PTA
随机推荐
[ruoyi] set theme style
Function knowledge points
Single instance mode of encapsulating PDO with PHP in spare time
resulttype和resultmap的区别和应用场景
Deep parsing pointer and array written test questions
SD card reports an error "error -110 whilst initializing SD card
Codeworks 5 questions per day (1700 average) - day 6
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
Getting started with applet cloud development - getting user search content
Self made CA certificate and SSL certificate using OpenSSL
Audio-AudioRecord Binder通信机制
有没有完全自主的国产化数据库技术
IPv6 comprehensive experiment
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
SAP ALV颜色代码对应颜色(整理)
ERA5再分析资料下载攻略
Redis SDS principle
ArabellaCPC 2019(补题)
Taobao focus map layout practice
手写数据库客户端