導航:首頁 > 編程語言 > 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排序相關的資料

熱點內容
漣源雲伺服器 瀏覽:992
linux中代表什麼 瀏覽:33
androidphone模塊 瀏覽:35
linux中斷表 瀏覽:177
文本寫的java如何編譯運行 瀏覽:524
單片機pwm控制舵機 瀏覽:318
壓縮機sd是什麼 瀏覽:772
php獲取數據轉json數據 瀏覽:525
程序員入職比期望薪資高 瀏覽:889
蘋果電腦怎麼在桌面建新文件夾 瀏覽:526
蘋果上不小心買錯了app怎麼撤銷 瀏覽:845
蘋果手機在設置怎麼卸載app 瀏覽:317
我的世界基岩版正版伺服器地址 瀏覽:562
vci命令 瀏覽:834
快剪輯app如何提取音樂 瀏覽:950
gcc編譯器適合單片機開發嗎 瀏覽:886
自做app如何聯網 瀏覽:303
vs的編譯運行 瀏覽:89
app反編譯後怎麼看代碼 瀏覽:766
蘇寧程序員需要注意什麼 瀏覽:557