Skip to content

Commit 8b4a1cc

Browse files
authored
Create Count the number of words in a string using HashMap.java
1 parent cb834a8 commit 8b4a1cc

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.HashMap;
2+
3+
public class FinalCountWords {
4+
5+
public static void main(String[] args) {
6+
// TODO Auto-generated method stub
7+
String str = "You are doing this Code";
8+
String[] split = str.split(" ");
9+
10+
HashMap<String,Integer> map = new HashMap<String,Integer>();
11+
for (int i=0; i<split.length; i++) {
12+
if (map.containsKey(split[i])) {
13+
int count = map.get(split[i]);
14+
map.put(split[i], count+1);
15+
}
16+
else {
17+
map.put(split[i], 1);
18+
}
19+
}
20+
System.out.println(map);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)