浏览代码

wordcountmap

Seth 5 年前
父节点
当前提交
89d9f3943e
共有 1 个文件被更改,包括 9 次插入2 次删除
  1. 9
    2
      src/main/java/rocks/zipcode/quiz5/collections/WordCounter.java

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

1
 package rocks.zipcode.quiz5.collections;
1
 package rocks.zipcode.quiz5.collections;
2
 
2
 
3
-import java.util.Map;
3
+import java.util.*;
4
 
4
 
5
 public class WordCounter {
5
 public class WordCounter {
6
+    private List<String> wordCounter;
6
     public WordCounter(String... strings) {
7
     public WordCounter(String... strings) {
8
+        wordCounter = new ArrayList<>(Arrays.asList(strings));
7
     }
9
     }
8
 
10
 
9
     public Map<String, Integer> getWordCountMap() {
11
     public Map<String, Integer> getWordCountMap() {
10
-        return null;
12
+        Map<String, Integer> wordCountMap = new HashMap<>();
13
+        for(String each: wordCounter){
14
+            int count = wordCountMap.containsKey(each) ? wordCountMap.get(each) : 0;
15
+            wordCountMap.put(each, count +1);
16
+        }
17
+        return wordCountMap;
11
     }
18
     }
12
 }
19
 }