遍历一个字符串字母出现的次数
package Ch17; import java.util.Map; import java.util.HashMap; import static javax.swing.UIManager.put; /** * Created by liwenj on 2017/7/28. */ public class Test22 { public static void main(String[] args) { String temp = "asdfghklsd"; char[] chars = temp.toCharArray(); Map<Character, Integer> result = new HashMap<>(); for (char c : chars) { boolean f=result.containsKey(c); if (f) { Integer old=result.get(c); result.put(c,old+1); } else { result.put(c,1); } } System.out.println(result); } }
运行结果如下: