日期:2014-05-17 浏览次数:20903 次
public class Test { public static void main(String[] args) { String[] strs = new String[]{"a","b","a"}; Map<String, Integer> map = new HashMap<String, Integer>(); for(String s : strs){ Integer key = map.get(s); if(key == null){ map.put(s, 1); }else{ map.put(s, key.intValue() + 1); } } Set<Entry<String, Integer>> set = map.entrySet(); List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(); Iterator<Entry<String, Integer>> iter = set.iterator(); while(iter.hasNext()){ list.add(iter.next()); } Collections.sort(list, new ComparatorEntry()); for(Entry<String, Integer> entry : list){ System.out.println(entry.getKey() + " : " + entry.getValue()); } } } class ComparatorEntry implements Comparator<Entry<String, Integer>>{ @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { return o2.getValue().intValue() - o1.getValue().intValue(); } }