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

熱點內容
圖解政治pdf 瀏覽:162
自製可編程計算機 瀏覽:132
每個文件夾放入一張圖片 瀏覽:453
cgzip壓縮文件夾 瀏覽:591
如何找下載的app 瀏覽:470
程序員之死最新進展 瀏覽:261
台電u盤可以下載加密嗎 瀏覽:697
java培訓機構上海 瀏覽:616
長城伺服器怎麼裝機 瀏覽:355
pm和hm哪個軟體編程好 瀏覽:205
咋樣把文件夾強力去除 瀏覽:989
jvmgc演算法 瀏覽:599
我的世界怎樣控制命令方塊發文字 瀏覽:863
期貨翻番計演算法 瀏覽:495
華為底部app如何增加 瀏覽:115
為什麼用了幾個星期的安卓充電器就壞了 瀏覽:358
伺服器風扇響怎麼回事 瀏覽:16
django打包編譯 瀏覽:196
u盤加密碼視頻 瀏覽:165
cmake創建文件夾 瀏覽:935