嗨,我有一个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);