找出字符串中出现次数最多的字符

private static void countcharecter(String str) {
		char[] chars = str.toCharArray();
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		for (Character c : chars) {
			if (Character.isWhitespace(c))
				continue;
			if (map.containsKey(c) == false) {
				map.put(c, 1);
			} else {
				map.put(c, map.get(c) + 1);
			}
		}

		Set<Character> set = map.keySet();
		Iterator<Character> it = set.iterator();
		int count = 0;
		Character cMax = ' ';
		while (it.hasNext()) {
			Character c = (Character) it.next();
			System.out.println(c + ":" + map.get(c));
			if (map.get(c) > count) {
				cMax = c;
				count = map.get(c);
			}
		}
		System.out.println("最多出现的是:" + cMax + ":" + count);
	}

你可能感兴趣的:(java 字符统计)