Occurences.java (772B)
1 import java.util.ArrayList; 2 import java.util.HashMap; 3 import java.util.Map; 4 5 public class Occurences { 6 public static void main(String[] args) { 7 ArrayList<String> strs = new ArrayList<String>(); 8 HashMap<String, Integer> occ = new HashMap<String, Integer>(); 9 10 strs.add("foo"); 11 strs.add("foo"); 12 strs.add("foo"); 13 strs.add("bar"); 14 strs.add("baz"); 15 strs.add("baz"); 16 17 for (String s : strs) 18 occ.put(s, 1 + (occ.containsKey(s) ? occ.get(s) : 0)); 19 for (Map.Entry<String, Integer> e : occ.entrySet()) 20 System.out.println(e.getKey() + ": " + e.getValue()); 21 } 22 }