- class NumberCount({
- public static void main(String[] args) {
- numberCount();
- }
- private static void numberCount() {
- // TODO Auto-generated method stub
- String str = "lakjsdlkgjljljlasf我们,他们,,,。。。。";
- char[] arr = str.toCharArray();
-
- Map<Character,Integer> map = new TreeMap<Character,Integer>();
-
- for (int i = 0; i < arr.length; i++) {
- Integer value = map.get(arr[i]);
- if(value != null ){
- value = value + 1;
- map.put(arr[i], value);
- }else{
- map.put(arr[i], 1);
- }
- }
- System.out.println(map);
-
- List<Map.Entry<Character,Integer>> mapList = new ArrayList<Entry<Character, Integer>>(map.entrySet());
-
- Collections.sort(mapList,new Comparator<Map.Entry<Character,Integer>>(){
- @Override
- public int compare(Map.Entry<Character, Integer> e1,
- Map.Entry<Character, Integer> e2) {
- // TODO Auto-generated method stub
- return e1.getValue().compareTo(e2.getValue());
- }
-
-
- });
-
- System.out.println(mapList);
- for (Entry<Character, Integer> entry : mapList) {
- System.out.print(entry.getKey()+"("+entry.getValue()+ ")" +" ");
- }
-
- }
- }
复制代码
|
|