取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为: a(2)b(1)k(2)

package com.heima.test;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.TreeMap;

public class Test1 {

    /** 取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为: a(2)b(1)k(2) * @param args * @throws IOException */
    public static void main(String[] args) throws IOException {
        String str = "asdzxclkjoiwermsnasd12432452";
        HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
        char [] cc = str.toCharArray();
        for (char c : cc) {
            if(!hm.containsKey(c)){
                hm.put(c, 1);
            }else{
                hm.put(c, hm.get(c)+ 1);
            }
        }
        for (char c : hm.keySet()) {
            System.out.print(c+"("+hm.get(c)+")");
        }
        FileWriter fw = new FileWriter("bbb.txt");
        for (char c : hm.keySet()) {
            fw.write(c+"("+hm.get(c)+")");
            fw.write("\n");
        }
        fw.close();
    }

}

你可能感兴趣的:(取出一个字符串中字母出现的次数。如:字符串:"abcdekka27qoq" ,输出格式为: a(2)b(1)k(2))