This question has already been answered:
- Simple if else & 3 response syntax
ArrayList<String> words = new ArrayList<String>(); for (int i = 0; i < 20; i++) words.add(reader.readLine()); Map<String, Integer> map = countWords(words); for (Map.Entry<String, Integer> pair : map.entrySet()) System.out.println(pair.getKey() + " " + pair.getValue()); public static Map<String, Integer> countWords(ArrayList<String> list){ HashMap<String, Integer> result = new HashMap<String, Integer>(); for (String word: list) result.put(word, result.containsKey(word) ? result.get(word) + 1: 1); } decipher, please, this line:
result.put(word, result.containsKey(word) ? result.get(word) + 1: 1);