Java求字符串中出現次數最多的字符
【
尊重
原創,轉載請注明出處】http://blog.csdn.net/guyuealian/article/details/51933611
Java求字符串中出現次數最多的字符,如String Str = "aaabbcddddee";那么輸出:d 4 ;若String Str = "aaabbcddddeexxxxxx";那么輸出:x 6
【思路】:首先將字符串拆分為字符數組,然后轉存到HashMap集合中,該集合的key為字符串中出現的字符,value對應該字符出現的次數。最后只需要在HashMap集合中找到Value值最大的key即可。實現代碼如下(PS:輸出形式若有變化,自己修改哈)
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- public class JavaTest {
- public static void main(String[] args) throws Exception {
- String Str = "AAbbcccaaaa";
- char[] StrArr = Str.toCharArray();// 把字符串轉為字符數組toCharArray
- Map<Character, Integer> map = MapFunction(StrArr);
- char ch = FindMapMaxValue(map);
- }
- /**
- * MapFunction:實現將字符數組轉存到Map中, 其中,Map中的key為出現的字符,value對應該字符出現的次數
- * @param StrArr StrArr字符數組,輸入前必須先將字符串轉為字符數組
- * @return map 集合中,key為出現的字符(Character),value對應該字符出現的次數(Integer)
- */
- public static Map<Character, Integer> MapFunction(char[] StrArr) {
- Map<Character, Integer> map = new HashMap<Character, Integer>();
- if (!(StrArr == null || StrArr.length == 0))// 先判斷字符數組是否為空
- for (int i = 0; i < StrArr.length; i++)
- if (null != map.get(StrArr[i]))
- // 若不為空,說明已經存在相同字符,則Value值在原來的基礎上加1
- map.put(StrArr[i], map.get(StrArr[i]) + 1);
- else
- map.put(StrArr[i], 1);
- return map;
- }
- /**
- * FindMapMaxValue 差找map中Value的最大值maxValue,類似於選擇排序尋找最大值的過程:
- * 先任取一個Value值定義為最大值,然后與之比較
- * @param map 輸入Map集合,該集合key為出現的字符(Character),value對應該字符出現的次數(Integer)
- * @return maxKey 返回出現次數最多的字符
- */
- public static Character FindMapMaxValue(Map<Character, Integer> map) {
- Set<Character> keys = map.keySet();// 獲得所有key值
- Iterator keys_Itera = keys.iterator();// 實例化Iterator
- // keys_Itera.next():依次獲得key值
- // map.get(key):獲得對應的value值
- Character maxKey = (Character) keys_Itera.next();// 定義第一個為最大value和對應的key
- int maxValue = map.get(maxKey);
- while (keys_Itera.hasNext()) {
- Character temp = (Character) keys_Itera.next();
- if (maxValue < map.get(temp)) {
- maxKey = temp;
- maxValue = map.get(temp);
- }
- }
- System.out.println("出現次數最多的字符:" + maxKey + " 出現次數:" + maxValue);
- return maxKey;
- }
- }
上面的FindMapMaxValue方法,還可以使用Map.Entry提高效率,進一步優化為:
- public static char FindMapMaxValue(Map<Character, Integer> map) {
- Iterator iter = map.entrySet().iterator();
- Map.Entry entry = (Map.Entry) iter.next();// 將第一個entry定義為最大次數的
- char maxKey = (char) entry.getKey();// 獲得K
- int maxValue = (int) entry.getValue();// 獲得V
- while (iter.hasNext()) {
- entry = (Map.Entry) iter.next();// 第二個entry
- char tempK = (char) entry.getKey();
- int tempV = (int) entry.getValue();
- if (maxValue < tempV) {
- maxKey = tempK;
- maxValue = tempV;
- }
- }
- System.out.println("出現次數最多的字符:" + maxKey + " 出現次數:" + maxValue);
- return maxKey;
- }
- import java.util.HashMap;
- import java.util.Map;
- public class JavaTest {
- public static void main(String[] args) throws Exception {
- String Str = "aaabbcddddee";
- char[] StrArr = Str.toCharArray();// 把字符串轉為字符數組toCharArray
- Map<Character, Integer> map = new HashMap<Character, Integer>();
- if (!(StrArr == null || StrArr.length == 0))// 先判斷字符數組是否為空
- for (int i = 0; i < StrArr.length; i++)
- if (null != map.get(StrArr[i]))
- // 若不為空,說明已經存在相同字符,則Value值在原來的基礎上加1
- map.put(StrArr[i], map.get(StrArr[i]) + 1);
- else
- map.put(StrArr[i], 1);
- // 找map中Value的最大值maxValue,類似於選擇排序,尋找最大值的過程:
- // 先任取一個Value值定義為最大值,然后與之比較
- int maxValue = map.get(StrArr[0]);
- char ch = ' ';
- for (int j = 0; j < StrArr.length; j++)
- if (maxValue < map.get(StrArr[j])) {
- maxValue = map.get(StrArr[j]);
- ch = StrArr[j];
- }
- System.out.println("現次數最多的字符:" + ch + " 出現次數:" + maxValue);
- }
- }
- 頂
- 0
- 踩