Java8对List<Map<String,String>>中元素排序降序
import java.util.*; public class Main { public static void main(String[] args) { List<Map<String, Object>> list = new ArrayList<>(); Map<String, Long> map = new HashMap<>(); map.put("count", 2L); Map<String, Long> map1 = new HashMap<>(); map1.put("count", 3L); list.add(map); list.add(map1); // 使用Java 8的Stream API进行排序 list.sort((map1, map2) -> { Long count1= (Long) map1.get("count"); Long count2 = (Long) map2.get("count"); return count2.compareTo(count1); // 降序排序 }); // 输出排序后的结果 for (Map<String, Object> m : list) { System.out.println(m); } } }
升序
retList.sort(map-> (Long) map1.get("count"));
降序
retList.sort((map1,map2)-> { Long r1 = (Long) map1.get("count"); Long r2 = (Long) map1.get("count"); return r2.compareTo(r1);//降序 });
获取降序结果
list = list.stream().sorted((map1,map2)->{ Long r1 = (Long)map1.get("count"); Long r2 = (Long)map2.get("count"); return r2.compareTo(r1); }).collect(Collectors.toList());