![](/img/trans.png)
[英]Implementing a sort of key-value pair (not essentially a HashMap) in Java
[英]How to sort a HashMap with key,value pair from an ArrayList in Java
嗨,我有一個 HashMap 的 ArrayList,我需要 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")));
假設您正確輸入 map:
HashMap<String, Integer> hashData = new HashMap<>();
hashData.put("mark", 1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.