很多场景下我们都要用到集合操作,同时要求我们按照一定的规则排序
public static void main(String[] args) {
Map<Integer, Character> map = new HashMap<>();
map.put(1, 'a');
map.put(2, 'b');
map.put(3, 'c');
map.put(4, 'c');
map.put(5, 'f');
map.put(6, 'e');
map.put(7, 'd');
List<Map.Entry<Integer, Character>> list = new ArrayList<>(map.entrySet());
list.sort(((o1, o2) -> o2.getValue() - o1.getValue()));
list.forEach(e->{
System.out.println(e.getKey() + "---" + e.getValue());
});
}
- 这是按照值排序,当然也可以按照key,当然条件不止一个,可以添加多个条件做相应的判断.
- 思路是先将其转化为list集合,再用比较器排序.
网友评论