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}

补充一个排序方法
	@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}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值