当前位置:网站首页>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 .
边栏推荐
- Résumé des méthodes de reconnaissance des caractères ocr
- [ruoyi] ztree custom icon (iconskin attribute)
- 【若依(ruoyi)】设置主题样式
- Explore pointers and pointer types in depth
- 适合程序员学习的国外网站推荐
- Jenkins basic knowledge ----- detailed explanation of 03pipeline code
- Misc (eternal night), the preliminary competition of the innovation practice competition of the National College Students' information security competition
- Safety science to | travel, you must read a guide
- 银行核心业务系统性能测试方法
- . Net 6 and Net core learning notes: Important issues of net core
猜你喜欢

Game theory matlab

Analyze 菜单分析

Problems encountered in 2022 work IV
![[concept] Web basic concept cognition](/img/27/14bcd73ca70d136436a4382a1b4bd1.jpg)
[concept] Web basic concept cognition

【概念】Web 基础概念认知

Research on cooperative control of industrial robots

MySQL advanced notes
![[unity3d] GUI control](/img/ef/9de2aa75c67cf825983867a913db74.png)
[unity3d] GUI control
![[kubernetes series] learn the exposed application of kubernetes service security](/img/61/4564230feeb988886fe595e3125ef4.png)
[kubernetes series] learn the exposed application of kubernetes service security

4. File modification
随机推荐
canvas切积木小游戏代码
Microservice registration and discovery
适合程序员学习的国外网站推荐
IPv6 jobs
Linear programming matlab
【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
Fault analysis | analysis of an example of MySQL running out of host memory
Crazy, thousands of netizens are exploding the company's salary
What is the investment value of iFLYTEK, which does not make money?
Is there a completely independent localization database technology
Sign SSL certificate as Ca
真机无法访问虚拟机的靶场,真机无法ping通虚拟机
Selenium share
Eight super classic pointer interview questions (3000 words in detail)
Leetcode problem solving -- 98 Validate binary search tree
[pointer training - eight questions]
Derivation of anti Park transform and anti Clarke transform formulas for motor control
What are the principles of software design (OCP)
有没有完全自主的国产化数据库技术
February 14, 2022 Daily: Google long article summarizes the experience of building four generations of TPU