如何在Java中使用ArrayList中的键值对对对HashMap进行排序



嗨,我有一个HashMap的ArrayList,我需要根据其键Value对HashMap进行排序。

ArrayList<HashMap> newList = new ArrayList();

循环启动:

HashMap hashData = new HashMap();
hashData.put("name", "string-studentname");
hashData.put("mark", "int-studentmark");
newList.add(hashData);

循环结束:

我需要newList按关键字标记进行排序。

我该怎么得到它?

如果ArrayList中有多个条目,并且密钥相同,则可以通过以下方式进行排序:

newList.sort(Comparator.comparingInt(o -> o.get("mark")));

假设你正确地输入了地图:

HashMap<String, Integer> hashData = new HashMap<>();
hashData.put("mark", 1);

最新更新