日期:2014-05-17 浏览次数:20823 次
public static void main(String[] args) { String str = "A,B,C,A,B,A"; Map<String, Integer> tm = new TreeMap<String, Integer>(); String[] words = str.split(","); for (final String s : words) { if (!s.isEmpty()) { if (!tm.containsKey(s)) { tm.put(s, 1); } else { tm.put(s, tm.get(s).intValue() + 1); } } } for (Map.Entry<String, Integer> entry : tm.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } }