这行字符串中出现频率最高的字符

1. 给定一行字符串,求出这行字符串中出现频率最高的字符,字符串中含有标点符号,字符不区分大小写。如果出现频率相同时,输出先出现在字符串中的字符。

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 import java.util.Map.Entry;
 4 import java.util.Scanner;
 5 public class Frequency {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         Scanner sc = new Scanner(System.in);
10         String input = sc.nextLine();
11         String strs = input.replaceAll(" ","").toLowerCase();
12         Map<Character, Integer> map = new HashMap<Character, Integer>();
13         Character CharStr = null;
14         Integer CountmaxLength = 0;
15         for (Character temp : strs.toCharArray()) {
16             if (map.containsKey(temp)) { 
17                 map.put(temp, map.get(temp) + 1);
18             } else {
19                 map.put(temp, 1);
20             }
21         }
22         for (Entry<Character, Integer> entry : map.entrySet()) {
23             if (entry.getValue() > CountmaxLength) {
24                 CharStr = entry.getKey();
25                 CountmaxLength = entry.getValue();
26             }    
27         }
28         
29         System.out.println(Character.toUpperCase(CharStr)+CountmaxLength.toString());
30     }
31 
32 }
原文地址:https://www.cnblogs.com/wangyufeiaichiyu/p/11238315.html