stream流对List<Map<String, Object>>集合根据value进行排序的一些 基本用法 第一步初始化数据/** * 初始化一个用于排序的集合出来 */ private ListMapString, Object getInitData(){ ListMapString, Object list new ArrayList(); MapString, Object map1 new HashMap(); map1.put(name, wangwu); map1.put(count, 41); map1.put(time, 15:15); list.add(map1); MapString, Object map2 new HashMap(); map2.put(name, zhaoliu); map2.put(count, 31); map2.put(time, 15:30); list.add(map2); MapString, Object map3 new HashMap(); map3.put(name, zhangsan); map3.put(count, 11); map3.put(time, 12:30); list.add(map3); MapString, Object map4 new HashMap(); map4.put(name, lisi); map4.put(count, 12); map4.put(time, 10:00); list.add(map4); return list; }第二步开始排序/** * 根据数字类型的value排序 */ Test public void sortByInt() { ListMapString, Object list getInitData(); list.forEach(c - System.out.println(排序前 c)); // 第一种如果value是int类型(1,2,3)可以直接强转然后进行排序 // list list.stream().sorted(Comparator.comparing(c - (Integer)c.get(count))).collect(Collectors.toList()); // 第二种如果value是String类型(1,2,3)需要先转成String之后再转换成int类型然后进行排序 list list.stream().sorted(Comparator.comparing(c - Integer.parseInt(c.get(count).toString()))).collect(Collectors.toList()); // 第三种 list list.stream().sorted((c1, c2) - { Integer count1 Integer.valueOf(c1.get(count).toString()); Integer count2 Integer.valueOf(c2.get(count).toString()); return count1.compareTo(count2); // 正序 // return count2.compareTo(count1); // 倒序 }).collect(Collectors.toList()); // 第四种上面这种方法可以直接使用String类型进行排序所以 list list.stream().sorted((c1, c2) - { String count1 c1.get(count).toString(); String count2 c2.get(count).toString(); return count1.compareTo(count2); // 正序 // return count2.compareTo(count1); // 倒序 }).collect(Collectors.toList()); // 最直接的逆转排序也可以 Collections.reverse(list); System.out.println( 分割线 ); list.forEach(c - System.out.println(排序后 c)); }打印结果排序前{namewangwu, count41, time15:15} 排序前{namezhaoliu, count31, time15:30} 排序前{namezhangsan, count11, time12:30} 排序前{namelisi, count12, time10:00} 分割线 排序后{namezhangsan, count11, time12:30} 排序后{namelisi, count12, time10:00} 排序后{namezhaoliu, count31, time15:30} 排序后{namewangwu, count41, time15:15}② 根据字符串排序/** * 根据字符串排序 (askii码表) */ Test public void sortByString() { ListMapString, Object list getInitData(); list.forEach(c - System.out.println(排序前 c)); list list.stream().sorted((c1, c2) - { // return c1.get(name).toString().compareTo(c2.get(name).toString()); String name1 c1.get(name).toString(); String name2 c2.get(name).toString(); return name1.compareTo(name2); // 正序 // return name2.compareTo(name1); // 倒序 }).collect(Collectors.toList()); // 逆转顺序 // Collections.reverse(list); System.out.println( 分割线 ); list.forEach(c - System.out.println(排序后 c)); }打印结果排序前{namewangwu, count41, time15:15} 排序前{namezhaoliu, count31, time15:30} 排序前{namezhangsan, count11, time12:30} 排序前{namelisi, count12, time10:00} 分割线 排序后{namelisi, count12, time10:00} 排序后{namewangwu, count41, time15:15} 排序后{namezhangsan, count11, time12:30} 排序后{namezhaoliu, count31, time15:30}③ 根据时间类型排序/** * 根据时间排序 */ Test public void sortByDate(){ ListMapString, Object list getInitData(); SimpleDateFormat sdf new SimpleDateFormat(HH:mm); list.forEach(c - System.out.println(排序前 c)); // 第一种直接使用String类型去排序 list list.stream().sorted(Comparator.comparing(c - c.get(time).toString())).collect(Collectors.toList()); // 第二种转换为时间类型然后再排序 list list.stream().sorted((c1, c2) -{ Date time1 null; Date time2 null; try { time1 sdf.parse(c1.get(time).toString()); time2 sdf.parse(c2.get(time).toString()); } catch (ParseException e) { e.printStackTrace(); } // return time1.compareTo(time2); // 正序 return time1.compareTo(time2); // 倒序 }).collect(Collectors.toList()); // 顺序逆转 Collections.reverse(list); System.out.println( 分割线 ); list.forEach(c - System.out.println(排序后 c)); }打印结果排序前{namewangwu, count41, time15:15} 排序前{namezhaoliu, count31, time15:30} 排序前{namezhangsan, count11, time12:30} 排序前{namelisi, count12, time10:00} 分割线 排序后{namelisi, count12, time10:00} 排序后{namezhangsan, count11, time12:30} 排序后{namewangwu, count41, time15:15} 排序后{namezhaoliu, count31, time15:30}补充一个排序方法Test public void sort() { ListMapString, Object list getInitData(); list.forEach(c - System.out.println(排序前 c)); Collections.sort(list, (o1, o2) - { return o1.get(count).toString().compareTo(o2.get(count).toString()); }); System.out.println( 分割线 ); list.forEach(c - System.out.println(排序后 c)); }打印结果排序前{namewangwu, count41, time15:15} 排序前{namezhaoliu, count31, time15:30} 排序前{namezhangsan, count11, time12:30} 排序前{namelisi, count12, time10:00} 分割线 排序后{namezhangsan, count11, time12:30} 排序后{namelisi, count12, time10:00} 排序后{namezhaoliu, count31, time15:30} 排序后{namewangwu, count41, time15:15}