使用stream流对List<Map<String, Object>>集合根据value进行排序

第一步:初始化数据

	/**
     * 初始化一个用于排序的集合出来
     */
    private List<Map<String, Object>> getInitData(){
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "wangwu");
        map1.put("count", "41");
        map1.put("time", "15:15");
        list.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "zhaoliu");
        map2.put("count", "31");
        map2.put("time", "15:30");
        list.add(map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("name", "zhangsan");
        map3.put("count", "11");
        map3.put("time", "12:30");
        list.add(map3);

        Map<String, 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() {
        List<Map<String, 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));
    }
打印结果
排序前===>>{name=wangwu, count=41, time=15:15}
排序前===>>{name=zhaoliu, count=31, time=15:30}
排序前===>>{name=zhangsan, count=11, time=12:30}
排序前===>>{name=lisi, count=12, time=10:00}
================== 分割线 ====================
排序后===>>{name=zhangsan, count=11, time=12:30}
排序后===>>{name=lisi, count=12, time=10:00}
排序后===>>{name=zhaoliu, count=31, time=15:30}
排序后===>>{name=wangwu, count=41, time=15:15}

② 根据字符串排序
	/**
     * 根据字符串排序 (askii码表)
     */
    @Test
    public void sortByString() {
        List<Map<String, 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));
    }
打印结果
排序前===>>{name=wangwu, count=41, time=15:15}
排序前===>>{name=zhaoliu, count=31, time=15:30}
排序前===>>{name=zhangsan, count=11, time=12:30}
排序前===>>{name=lisi, count=12, time=10:00}
================== 分割线 ====================
排序后===>>{name=lisi, count=12, time=10:00}
排序后===>>{name=wangwu, count=41, time=15:15}
排序后===>>{name=zhangsan, count=11, time=12:30}
排序后===>>{name=zhaoliu, count=31, time=15:30}

③ 根据时间类型排序
	/**
     * 根据时间排序
     */
    @Test
    public void sortByDate(){
        List<Map<String, 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));
    }
打印结果
排序前===>>{name=wangwu, count=41, time=15:15}
排序前===>>{name=zhaoliu, count=31, time=15:30}
排序前===>>{name=zhangsan, count=11, time=12:30}
排序前===>>{name=lisi, count=12, time=10:00}
================== 分割线 ====================
排序后===>>{name=lisi, count=12, time=10:00}
排序后===>>{name=zhangsan, count=11, time=12:30}
排序后===>>{name=wangwu, count=41, time=15:15}
排序后===>>{name=zhaoliu, count=31, time=15:30}
总结:String类型是万金油的排序方式,基本都可以用!!!



补充一个排序方法
	@Test
    public void sort() {
        List<Map<String, 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));
    }
打印结果
排序前===>>{name=wangwu, count=41, time=15:15}
排序前===>>{name=zhaoliu, count=31, time=15:30}
排序前===>>{name=zhangsan, count=11, time=12:30}
排序前===>>{name=lisi, count=12, time=10:00}
================== 分割线 ====================
排序后===>>{name=zhangsan, count=11, time=12:30}
排序后===>>{name=lisi, count=12, time=10:00}
排序后===>>{name=zhaoliu, count=31, time=15:30}
排序后===>>{name=wangwu, count=41, time=15:15}

就这样。。。

你可能感兴趣的:(java,list,java,排序算法)