将地图转换为已排序列表

时间:2012-11-01 20:50:43

标签: java collections guava

我正在尝试根据Map中的值将List转换为有序Map

假设我有以下内容:

Map<String, Integer> map = Maps.newHashMap();
map.put("foo", 1);
map.put("boo", 3);
map.put("bar", 2);

//list needs to be sorted by Integer Value -- ASC or DESC
List<String> list = //Elegant guava call? List should be: {foo, bar, boo}

1 个答案:

答案 0 :(得分:8)

List<String> list = Ordering.natural().onResultOf(Functions.forMap(map))
   .sortedCopy(map.keySet());

......我想?