我想知道我这段代码有什么问题吗,为什么获取到的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;
}
原代码
修改后的代码