Java Map is sorted by a value in value

How to sort a Map object, according to value or a certain attribute in the value object, the attribute is int type


Map<String, Object> map = new HashMap<>(); // Object可以换成实体类,按照实体类中的xxxInt进行升序排序
List<Map.Entry<String, Object>> listEntry = new ArrayList<>(map.getValue().entrySet());
Collections.sort(listEntry,(Comparator.comparing(o -> String.valueOf(o.getValue().xxxInt))));
Map<String, Object> map = new LinkedHashMap<>();

// 返回 map
listEntry.forEach(o -> {
    
    
	map.put(o.getKey(), o.getValue());
});

Guess you like

Origin blog.csdn.net/qq_43290288/article/details/129947914