Java求字符串中出现次数最多的字符
生活随笔
收集整理的這篇文章主要介紹了
Java求字符串中出现次数最多的字符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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();// 把字符串轉為字符數組toCharArrayMap<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值在原來的基礎上加1map.put(StrArr[i], map.get(StrArr[i]) + 1);elsemap.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和對應的keyint 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();// 獲得Kint maxValue = (int) entry.getValue();// 獲得Vwhile (iter.hasNext()) {entry = (Map.Entry) iter.next();// 第二個entrychar 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();// 把字符串轉為字符數組toCharArrayMap<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值在原來的基礎上加1map.put(StrArr[i], map.get(StrArr[i]) + 1);elsemap.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);} }
總結
以上是生活随笔為你收集整理的Java求字符串中出现次数最多的字符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ThreadLocal的理解
- 下一篇: java美元兑换,(Java实现) 美元