本帖最后由 李晓俊 于 2012-2-27 21:58 编辑
- public static void main(String[] args)
- {
- method3("deabbccfff");
-
- }
- public static void method3(String s)
- {
- Map<Character, Integer> map = new TreeMap<Character, Integer>();
-
- for(char c : s.toCharArray())
- map.put(c, map.containsKey(c) ? map.get(c) + 1 : 1);
- TreeSet<Entry<Character, Integer>> ts = new TreeSet<Entry<Character, Integer>>(new EntryComp());
- ts.addAll(map.entrySet());
- System.out.println(map);
- }下面是我的比较器
- class EntryComp implements Comparator<Entry<Character, Integer>>
- {
- public int compare(Entry<Character, Integer> en1,Entry<Character, Integer> en2)
- {
- int num = en1.getValue()-en2.getValue();
- return num==0 ? en1.getKey().compareTo(en2.getKey()) : num;
- }
- }
复制代码 需求是按照次数顺序排序, 次数相同的按照字母顺序: {a=1, d=1, e=1, b=2, c=2, f=3},,,怎么打印不出效果,,
打印结果为{a=1, b=2, c=2, d=1, e=1, f=3}
|