方法一:编写排序方法
public List<Map.Entry<String, String>> sortMap(Map<String, String> map) {
final List<Map.Entry<String, String>> infos = new ArrayList<Map.Entry<String, String>>(map.entrySet());
// 重写集合的排序方法:按字母顺序
Collections.sort(infos, new Comparator<Map.Entry<String, String>>() {
@Override
public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
return (o1.getKey().compareTo(o2.getKey()));
}
});
return infos;
}
方法二:使用stream的排序方法
List<Map.Entry<String, String>> sort = new ArrayList();
map.entrySet().stream().sorted(Map.Entry.<String, String>comparingByKey())
.forEachOrdered(paramEntry -> sort.add(paramEntry));