Write a program to count each unique words in a given string.
- public class WordCountByMapAlgoritham {
- public static void main(String[] args) {
- String s = "this is java code. this java code is highly recommended for java developer !!";
- String a[] = s.split(" ");
- Map<String, Integer> map = new HashMap<String, Integer>();
- for (String word : a) {
- if (map.get(word) != null) {
- map.put(word, map.get(word) + 1);
- } else
- map.put(word, 1);
- }
- System.out.println(map);
- }
- }
Output:
{!!=1, java=3, code=1, code.=1, this=2, for=1, is=2, developer=1, highly=1, recommended=1}
Comments
Post a Comment