Map ,List 顺序排列

1.List 顺序排列

 
  
List list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("b");
list.add("e");
//通过Collections.sort(List I,Comparator c)方法进行排序
Collections.sort(list, new Comparator() {
    @Override
    public int compare(String o1, String o2) {
        return o2.compareTo(o1);
    }
});
for (String s : list) {
    System.out.println(s);
}
2.Map 按照value 排序


Map, Integer> map_Data = new TreeMap<>();
map_Data.put("A", 98);
map_Data.put("D", 50);
map_Data.put("F", 76);
map_Data.put("B", 23);
map_Data.put("E", 85);
map_Data.put("C", 50);
System.out.println(map_Data);
//Map转化为List集合,List采用ArrayList
List, Integer>> list_Data = new ArrayList<>(map_Data.entrySet());
//通过Collections.sort(List I,Comparator c)方法进行排序
Collections.sort(list_Data,new Comparator, Integer>>() {
    @Override
    public int compare(Map.Entry, Integer> o1, Map.Entry, Integer> o2) {
        return (o1.getValue().compareTo(o2.getValue()));
    }
});
System.out.println(list_Data);

你可能感兴趣的:(Java)