当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
jsscript
Leetcode problem solving -- 108 Convert an ordered array into a binary search tree
【若依(ruoyi)】ztree 自定义图标(iconSkin 属性)
【Kubernetes 系列】一文学会Kubernetes Service安全的暴露应用
three.js网页背景动画液态js特效
js 正则过滤和增加富文本中图片前缀
3857墨卡托坐标系转换为4326 (WGS84)经纬度坐标
canvas切积木小游戏代码
Princeton University, Peking University & UIUC | offline reinforcement learning with realizability and single strategy concentration
StrError & PERROR use yyds dry inventory
SAP ALV单元格级别设置颜色
[ruoyi] ztree custom icon (iconskin attribute)
Rust language -- iterators and closures
codeforces每日5题(均1700)-第六天
The real machine cannot access the shooting range of the virtual machine, and the real machine cannot Ping the virtual machine
Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning
Deep parsing pointer and array written test questions
ArabellaCPC 2019(补题)
Single instance mode of encapsulating PDO with PHP in spare time
真机无法访问虚拟机的靶场,真机无法ping通虚拟机









