目前 Eclipse Collections 中没有可用的直接 API 来Map根据其值对 a 进行排序。
一种替代方法是将地图翻转为MutableSortedMapusing flipUniqueValues。
MutableSortedMap<Integer, String> sortedMap = SortedMaps.mutable.empty();
sortedMap.putAll(mutableMap.flipUniqueValues());
System.out.println(sortedMap);
这会给你一个按键MutableSortedMap排序的。Integer这里的输出将是:{1=One, 2=Two, 3=Three}
您也可以先存储Pairs,List然后使用String密钥对它们进行唯一分组以创建MutableMap. 如果 中的值Map是实例,则Pair它们可用于创建 sortedList或使用直接 API。SortedSetSortedBag
MutableList<Pair<String, Integer>> list = Lists.mutable.with(
Tuples.pair("Three", 3),
Tuples.pair("One", 1),
Tuples.pair("Two", 2)
);
MutableMap<String, Pair<String, Integer>> map =
list.groupByUniqueKey(Pair::getOne);
System.out.println(map);
MutableList<Pair<String, Integer>> sortedList =
map.toSortedListBy(Pair::getTwo);
MutableSortedSet<Pair<String, Integer>> sortedSet =
map.toSortedSetBy(Pair::getTwo);
MutableSortedBag<Pair<String, Integer>> sortedBag =
map.toSortedBagBy(Pair::getTwo);
System.out.println(sortedList);
System.out.println(sortedSet);
System.out.println(sortedBag);
输出:
{One=One:1, Three=Three:3, Two=Two:2}
[One:1, Two:2, Three:3]
[One:1, Two:2, Three:3]
[One:1, Two:2, Three:3]
上述所有toSorted方法仅对值进行操作。这就是我将值存储为Pair实例的原因。
注意:我是Eclipse Collections的提交者。