java中Hashmap按key排序

在实际的工作中,有时会有Hashmap按key排序 的需求,我找的方法如下:

一、

              <pre name="code" class="java"> HashMap<String, List<Integer>> raceMap = new HashMap<String, List<Integer>>();
 Object[] key_arrrObjects = raceMap.keySet().toArray();
		Arrays.sort(key_arrrObjects);
		for  (Object key : key_arrrObjects) {   
		    Object value = raceMap.get(key);   
		    System.out.println(value.toString());
		}

 
 

二、

Set<String> set =raceMap.keySet();
		ArrayList<String> arrayList =new ArrayList<String>(set);
		Collections.sort(arrayList);//从小到大的排序
		
		for (int len = arrayList.size(),i = len-1; i >= 0; i--) {
			 System.out.println("key键---值: "+arrayList.get(i)+","+raceMap.get(arrayList.get(i)));
}
			 



你可能感兴趣的:(java中Hashmap按key排序)