瀏覽代碼

143 of 151

Nick Satinover 5 年之前
父節點
當前提交
d3288fb8e8
共有 1 個檔案被更改,包括 6 行新增2 行删除
  1. 6
    2
      src/main/java/rocks/zipcode/quiz5/collections/WordCounter.java

+ 6
- 2
src/main/java/rocks/zipcode/quiz5/collections/WordCounter.java 查看文件

@@ -5,11 +5,15 @@ import java.util.Map;
5 5
 
6 6
 public class WordCounter {
7 7
     Map<String, Integer> map = new HashMap<>();
8
-    Integer counter = 0;
9 8
 
10 9
     public WordCounter(String... strings) {
11 10
         for (String s: strings) {
12
-            map.put(s, ++counter);
11
+            if (!map.containsKey(s)){
12
+                map.put(s, 1);
13
+            }
14
+            else {
15
+                map.put(s, map.get(s) + 1);
16
+            }
13 17
         }
14 18
     }
15 19