使用Stream对List<Map>排序

1、添加测试数据

    public static void main(String[] args) {
        List<Map> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        Person p = new Person("玛卡巴卡","s5",7);
        map.put("name",p.name);
        map.put("no",p.no);
        map.put("age",p.age);
        list.add(map);
        map = new HashMap<>();
        p = new Person("唔西迪西","s7",8);
        map.put("name",p.name);
        map.put("no",p.no);
        map.put("age",p.age);
        list.add(map);
        map = new HashMap<>();
        p = new Person("叮叮车","s1",4);
        map.put("name",p.name);
        map.put("no",p.no);
        map.put("age",p.age);
        list.add(map);
        list.forEach(System.out::println);

    }

    static class Person{
        String name;
        String no;
        Integer age;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNo() {
            return no;
        }

        public void setNo(String no) {
            this.no = no;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public Person(String name, String no, Integer age) {
            this.name = name;
            this.no = no;
            this.age = age;
        }

        @Override
        public String toString() {
            return "person{" +
                    "name='" + name + '\'' +
                    ", no='" + no + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

输出如下 

1.1 按照no排序

        System.out.println("排序前");
        list.forEach(System.out::println);

        System.out.println("排序后1");
        List<Map> no = list.stream().sorted(comparing(l ->     l.get("no").toString())).collect(Collectors.toList());
        no.forEach(System.out::println);

        System.out.println("排序后2");
        List<Map> no1 = list.stream().sorted((map1, map2) -> {
            String time1 = map1.get("no").toString();
            String  time2 = map2.get("no").toString();
            return time1.compareTo(time2);
        }).collect(Collectors.toList());
        no1.forEach(System.out::println);

        System.out.println("排序后3");
        List<Map> no2 = list.stream().sorted((map1, map2) -> {
            int time1 = Integer.parseInt(map1.get("no").toString().replace("s",""));
            int  time2 = Integer.parseInt(map2.get("no").toString().replace("s",""));
            return time1 - time2;
        }).collect(Collectors.toList());
        no2.forEach(System.out::println);

 输出

使用(map1,map2)时,注意顺序就好

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值