public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 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 (int i = 0; i < list.size(); i++) { if(result.containsKey(list.get(i))){ int count=result.get(list.get(i)); count++; result.put(list.get(i),count); }else { result.put(list.get(i),1); } } return result; } }