属性get方法一直是空

我想知道我这段代码有什么问题吗,为什么获取到的sort一直是空的?
项目使用的是spring boot

public class ReadHistoryDTO extends ReadHistory {
 private Map<String, String> sort;
 public Map<String, String> getSort() {
        Map<String, String> newSort = new HashMap<>();
        newSort.putAll(sort);
        Iterator iterator = newSort.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            if (entry.getKey() == null || !StringUtils.hasText(entry.getKey().toString()))
                continue;
            String key = entry.getKey().toString();
            if (StringUtils.hasText(key) && !key.contains("_")) {

                ReadHistory readHistory = new ReadHistory();
                Boolean isExist = CheckObjectUtils.isExistField(key, readHistory);
                if (isExist != null && isExist) {
                    int temp = 0;
                    StringBuilder orderSb = new StringBuilder(key);

                    for (int i = 0; i < key.length(); i++) {
                        char c = key.charAt(i);
                        if (Character.isUpperCase(c)) { // isLowerCase(): true--小写字符。isUpperCase:true--大写字符
                            orderSb.insert(i + temp, "_");
                            temp += 1;
                        }
                    }

                    key = orderSb.toString().toLowerCase();
                } else {
                    newSort.remove(key);
                    continue;
                }
            } else if (StringUtils.hasText(key) && key.contains("_")) {
                String keyAry[] = key.split("_");
                if (keyAry.length > 2) {
                    newSort.remove(key);
                    continue;
                }

                String keyArySub = keyAry[1];
                keyArySub = keyArySub.substring(0, 1).toUpperCase() + keyArySub.substring(1).toLowerCase();
                String newKey = keyAry[0] + keyArySub;
                ReadHistory readHistory = new ReadHistory();
                Boolean isExist = CheckObjectUtils.isExistField(newKey, readHistory);
                if (isExist == null || !isExist) {
                    newSort.remove(key);
                    continue;
                }
            }

            if (entry.getValue() == null) {
                newSort.remove(key);
                continue;
            }
            String value = entry.getValue().toString();
            if (!value.equals("desc") && value.equals("asc")) {
                newSort.remove(key);
                continue;
            }
        }
        return newSort;
    }

public void setSort(Map<String, String> sort) {
        this.sort = sort;
    }
}

知道原因了,因为map删除数据的时候不对,因为遍历的时候删除数据必须是迭代删除。正确代码应该是这样的

public Map<String, String> getSort() throws Exception {
        Map<String, String> newSort = new HashMap<>();
        newSort.putAll(sort);
        Iterator iterator = newSort.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry entry = (Map.Entry) iterator.next();
            if (entry.getKey() == null || !StringUtils.hasText(entry.getKey().toString()))
                continue;
            String key = entry.getKey().toString();
            if (StringUtils.hasText(key) && !key.contains("_")) {

                ReadHistory readHistory = new ReadHistory();
                Boolean isExist = CheckObjectUtils.isExistFieldName(key, readHistory);
                if (isExist != null && isExist) {
                    int temp = 0;
                    StringBuilder orderSb = new StringBuilder(key);

                    for (int i = 0; i < key.length(); i++) {
                        char c = key.charAt(i);
                        if (Character.isUpperCase(c)) { // isLowerCase(): true--小写字符。isUpperCase:true--大写字符
                            orderSb.insert(i + temp, "_");
                            temp += 1;
                        }
                    }

                    key = orderSb.toString().toLowerCase();
                    iterator.remove();
                    newSort.put(key, entry.getValue().toString());
                } else {
                    iterator.remove();
                    continue;
                }
            } else if (StringUtils.hasText(key) && key.contains("_")) {
                String keyAry[] = key.split("_");
                if (keyAry.length > 2) {
                    iterator.remove();
                    continue;
                }

                String keyArySub = keyAry[1];
                keyArySub = keyArySub.substring(0, 1).toUpperCase() + keyArySub.substring(1).toLowerCase();
                String newKey = keyAry[0] + keyArySub;
                ReadHistory readHistory = new ReadHistory();
                Boolean isExist = CheckObjectUtils.isExistField(newKey, readHistory);
                if (isExist == null || !isExist) {
                    iterator.remove();
                    continue;
                }
            }

            if (entry.getValue() == null) {
                iterator.remove();
                continue;
            }
            String value = entry.getValue().toString();
            if (!value.equals("desc") && value.equals("asc")) {
                iterator.remove();
                continue;
            }
        }

        return newSort;
    }

原代码
在这里插入图片描述
修改后的代码
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值