1 package com.tn.hashMap;
2
3 import java.util.HashMap;
4 import java.util.TreeSet;
5
6 public class HashMapDemo {
7 public static void main(String[] args){
8
9 String str="eeeeaccbbddddffffff";
10 HashMap<String,Integer> hashMap=new HashMap<String,Integer>();
11 for(int i=0;i<str.length();i++){
12 if(!hashMap.containsKey(str.substring(i, i+1))){
13 hashMap.put(str.substring(i, i+1), 1);
14 }else{
15 hashMap.put(str.substring(i,i+1), hashMap.get(str.substring(i, i+1))+1);
16 }
17 }
18
19 TreeSet<T> treeSet=new TreeSet<T>();
20 for(String key:hashMap.keySet()){
21 T t=new T(key,hashMap.get(key));
22 treeSet.add(t);
23 }
24 System.out.println(treeSet);
25 }
26 }
27
28 class T implements Comparable<T>{
29 private String value;
30 private Integer count;
31
32 public T(String value, Integer count) {
33 super();
34 this.value = value;
35 this.count = count;
36 }
37
38 public String getValue() {
39 return value;
40 }
41
42 public void setValue(String value) {
43 this.value = value;
44 }
45
46 public Integer getCount() {
47 return count;
48 }
49
50 public void setCount(Integer count) {
51 this.count = count;
52 }
53
54 @Override
55 public String toString() {
56 return value+"出现"+count+"次。";
57 }
58
59 @Override
60 public int compareTo(T o) {
61 if(this.count!=o.count)
62 return this.count-o.count;//以出现次数排序
63 else
64 return this.value.compareTo(o.value);//出现次数相同以字母顺序排序
65 }
66
67 }