1 package com.kingdee.eas.hr.perf.client; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.HashMap; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.Map; 10 import java.util.Set; 11 import java.util.TreeSet; 12 13 //add by ypf on 20140410 根据map的key进行排序 14 public class MapSort { 15 16 private HashMap map = new HashMap(); 17 private Set keySet = map.keySet(); 18 19 public Object get(String key) { 20 return map.get(key); 21 } 22 23 public void put(String key, Object value) { 24 map.put(key, value); 25 } 26 27 public Set sort(Map map) { 28 List list = new ArrayList(map.keySet()); 29 30 Collections.sort(list, new Comparator() { 31 public int compare(Object a, Object b) { 32 return a.toString().toLowerCase().compareTo( 33 b.toString().toLowerCase()); 34 } 35 }); 36 37 this.keySet = new TreeSet(list); 38 return keySet; 39 } 40 41 public Set keySet() { 42 return this.keySet; 43 } 44 45 public static void main(String[] args) { 46 MapSort map = new MapSort(); 47 map.put("1", "yi"); 48 map.put("8", "ba"); 49 map.put("9", "jiu"); 50 map.put("7", "qi"); 51 map.put("5", "wu"); 52 map.put("6", "liu"); 53 map.put("4", "si"); 54 map.put("3", "san"); 55 map.put("2", "er"); 56 57 for (Iterator it = map.keySet().iterator(); it.hasNext();) { 58 String key = (String) it.next(); 59 // System.out.println( "key[ " + key + "], value[ " + map.get(key) 60 // + "] "); 61 } 62 63 // System.out.println( "\n "); 64 // map.sort(); 65 for (Iterator it = map.keySet().iterator(); it.hasNext();) { 66 String key = (String) it.next(); 67 System.out.println("key[ " + key + "], value[ " + map.get(key) 68 + "] "); 69 } 70 } 71 }