import java.util.TreeMap; //计算字符串中子串出现的次数 public class Test { public static void main(String[] args) { TreeMap<Character,Integer > tm = new TreeMap<>(); String s = "nalskjoiw eaoijajkwlcx aksoiaa lzndoiawlksnnz"; char[] chs = s.toCharArray(); for (char c : chs) { if(!tm.containsKey(c)){ tm.put(c, 1); }else{ tm.put(c, tm.get(c)+1); } } for (Character c : tm.keySet()) { System.out.println(c + "---" +tm.get(c)); } } }