導航:首頁 > 編程語言 > java的hashmap排序

java的hashmap排序

發布時間:2023-02-24 20:48:19

『壹』 java里,想對hashmap里的key進行排序

chNNN -> NNN -> Integer.parseInt(NNN)
"ch10" -> "10" -> Integer.parseInt("10") -> 10
--------
Yes, you are right, but not too slow/difficult:

import java.util.*;

public class Tmp {
public static void main(String[] args) {
Map<String, Object> result = new HashMap<String, Object>();
result.put("ch1", "ch 1");
result.put("ch111", "ch 111");
result.put("ch11", "ch 11");
result.put("ch2", "ch 2");
result.put("ch13", "ch 13");
Object[] keys=result.keySet().toArray();
Comparator<Object> c = new Comparator<Object>(){
@Override
public int compare(Object o1, Object o2) {
//TODO: add argument check yourself
int a1 = Integer.parseInt(((String)o1).substring(2));
int a2 = Integer.parseInt(((String)o2).substring(2));
if(a1>a2)return 1;
if(a1==a2)return 0;
else return -1;
}
};
Arrays.sort(keys, c);
for(Object s:keys){
System.out.println(s +"=>"+result.get(s));
}
}
}

閱讀全文

與java的hashmap排序相關的資料

熱點內容
高通at指令源碼 瀏覽:516
疫就是命令 瀏覽:47
php讀取根目錄 瀏覽:52
杜魯門pdf 瀏覽:15
窪池指標公式源碼 瀏覽:900
編譯程序添加內核 瀏覽:637
如何讓吃雞蘋果號登錄安卓號 瀏覽:72
伺服器名稱怎麼查詢 瀏覽:432
直播要怎麼推廣app 瀏覽:76
安卓10id如何計算 瀏覽:601
求3的倍數python 瀏覽:675
江蘇好伺服器優質推薦雲空間 瀏覽:139
用php做一個留言板 瀏覽:226
app灰色頁面怎麼回事 瀏覽:57
最長連續遞減子序列python 瀏覽:154
電腦編譯系統有哪些 瀏覽:914
新建的文件夾另一部手機讀不出來 瀏覽:989
怎麼把安卓app傳到新iphone 瀏覽:37
安卓伺服器如何關閉 瀏覽:581
linux登陸不了 瀏覽:553