简体   繁体   English

如何按值对Java Map进行排序,如果value是then by键

[英]How to sort Java Map by value and if value is the then by key

I need to sort this TreeMap by values and if value is the same like 1 or 2 in this case then I need to sort these elements by their keys. 我需要按值对TreeMap进行排序,并且在这种情况下,如果value像1或2一样,则需要按其键对这些元素进行排序。 How can I do it? 我该怎么做?

{65=2, 67=1, 69=4, 72=2, 73=5, 77=1, 78=2, 79=3, 81=1, 83=7, 84=6, 85=2, 87=2, 89=1}

So the result would looks like this 所以结果看起来像这样

{83=7, 84=6, 73=5, 69=4, 79=3, 65=2, 72=2, 78=2, 85=2, 87=2, 67=1, 77=1, 81=1, 89=1}

Thank you for help. 谢谢你的帮助。

You can't actually sort a TreeMap by its values intuitively. 您实际上无法直观地按其值对TreeMap进行排序。 This post on the site explains how to tackle your predicament. 网站上的帖子说明了如何解决您的困境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM