map中的key进行排序
Map<String, String> value = entry.getValue();
Map<String, String> collect = value.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (oldVal, newVal) -> oldVal, LinkedHashMap::new));
若数据是 List<Map<String, Object>> value,对其进行排序:
private List<Map<String, Object>> dealSorted(List<Map<String, Object>> value) {
Collections.sort(value, new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
String cserialno1 = o1.get("serialNo").toString();
String cserialno2 = o2.get("serialNo").toString();
return cserialno2.compareTo(cserialno1);
}
});
return value;
}
使用Java流来做排序:
public static void main(String[] args) {
Map<String, Integer> codes = new HashMap<>();
codes.put("United States", 1);
codes.put("Germany", 49);
codes.put("France", 33);
codes.put("China", 86);
codes.put("Pakistan", 92);
Map<String, Integer> sortedMap = codes.entrySet().stream().sorted(Map.Entry.comparingByKey()).collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(oldVal, newVal) -> oldVal,
LinkedHashMap::new
)
);
sortedMap.entrySet().forEach(System.out::println);
}
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习