我有一个列表,其中的元素是 map ,我想根据 map 元素值对该列表进行排序
[{id=45964953, count=1}, {id=46009636, count=1}, {id=45936991, count=1}, {id=45984035, count=2}, {id=45951961, count=1}, {id=45399668, count=31}]
我需要根据计数值对其进行排序。 java可以实现吗
输出应该是这样的
[ {id=45399668, count=31},{id=45984035, count=2}, {id=45964953, count=1}, {id=46009636, count=1}, {id=45936991, count=1}, {id=45951961, count=1}]
最佳答案
您可以创建一个实现 Comparable<Map<String,Integer>>
的类(假设 String
和 Integer
以及您的映射的键和值)并根据您的标准比较两个映射。
然后您可以将列表和比较器传递给 Collections.sort()。
关于java - 对以 map 作为输入的列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28165035/