【发布时间】:2016-07-26 22:26:14
【问题描述】:
我有一个Map<String,Integer>,它按如下值排序:
set = map.entrySet();
list = new ArrayList<Map.Entry<String, Integer»(set);
Collections.sort( list, (o1, o2) -> (o2.getValue()).compareTo( o1.getValue() ));
我已经有一个来自该地图的排序整数列表:
word_used = new ArrayList<Integer>(map.values()
.stream()
.sorted()
.collect(Collectors.toList()));
Collections.reverse(word_used);
但是我怎样才能得到一个字符串列表,它将被排序等于 Map(按值)?
我的意思是,如果我有一张包含物品的地图:
map.put("eggs",1500);
map.put("echo",150);
map.put("foo",320);
map.put("smt",50);
并按如下方式排序:
eggs : 1500
foo : 320
echo : 150
smt : 50
我需要获取 2 个列表:
eggs
foo
echo
smt
和
1500
320
150
50
【问题讨论】:
-
word_list = new ArrayList<String>(map.keySet() .stream() .sorted() .collect(Collectors.toList()));工作吗? -
请注意,说
Map已排序是一种误导。构造和排序Map的条目的List不会影响Map集合视图的迭代顺序。您只有一个已排序的List——尽管如此,它还是很有用的。
标签: java list dictionary