java实现英文字母统计

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;


public class Test2 {

	public Test2() {
		// TODO Auto-generated constructor stub
	}
	public HashMap<String, Integer> getcount(String content) {
		String chars = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
		String[] char_arry_tolower = chars.split(",");
		List<String> list = new ArrayList<String>();
		for (String char_array : char_arry_tolower) {
			list.add(char_array); //小写
			list.add(char_array.toUpperCase()); //变成大写
		}
		HashMap<String, Integer> hash = new HashMap<String, Integer>();
		for (int i=0;i<content.length();i++) {
			char ch = content.charAt(i);
			for (String str : list) {
				if (String.valueOf(ch).equals(str)) {
					if (hash.get(str) != null) {
						int count = hash.get(str);
						count++;
						hash.put(str, count);
					} else {
						hash.put(str, 1);
					}
				}
			}
		}
		return hash;
	}
	public static void main(String[] args) {
		Test2 test2 = new Test2();
		String content = "Now is the time for all good men to come to the aid of their country";
		HashMap<String,Integer> hash = test2.getcount(content);
		Set<String> set = hash.keySet();
		for (String string : set) {
			System.out.println(string+"===>"+hash.get(string));
		}
	}
}

你可能感兴趣的:(java,C++,c,C#,F#)