Java8对List<Map<String,String>>自定义排序

List<Map<String,String>> arrayList = new ArrayList<>();

        Map<String, String> securityMap1 = new HashMap<>();
        securityMap1.put("name","期货");
        securityMap1.put("value","100.11");
        arrayList.add(securityMap1);

        Map<String, String> securityMap2 = new HashMap<>();
        securityMap2.put("name","基金");
        securityMap2.put("value","200.22");
        arrayList.add(securityMap2);

        Map<String, String> securityMap3 = new HashMap<>();
        securityMap3.put("name","股票");
        securityMap3.put("value","300.33");
        arrayList.add(securityMap3);

        Map<String, String> securityMap4 = new HashMap<>();
        securityMap4.put("name","债券");
        securityMap4.put("value","400.44");
        arrayList.add(securityMap4);

        List<String> sortList = Arrays.asList("股票", "债券", "基金", "期货");
        List<Map<String, String>> result = arrayList.stream().sorted(Comparator.comparing(e -> e.get("name"), (x, y) -> {
            //按照读取的list顺序排序
            for (String sort : sortList) {
                if (sort.equals(x) || sort.equals(y)) {
                    if (x.equals(y)) {
                        return 0;
                    } else if (sort.equals(x)) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
            }
            return 0;
        })).collect(Collectors.toList());
        System.out.println(result);

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值