Java Program to Count Word in String

import java.util.HashMap;

public class CountWordInString {
public static void main(String[] args) {
CountWordInString f = new CountWordInString();
f.countWordInString();

}

public void countWordInString() {
String str = "i am am ankit ankit ankit vijay vijay vijay";
String[] split = str.split(" ");
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < split.length; i++) {
if (map.containsKey(split[i])) {
int count = map.get(split[i]);
map.put(split[i], count + 1);

}

else {
map.put(split[i], 1);
}
}
System.out.println(map);

}
}


Output
----------------
{ankit=3, i=1, am=2, vijay=3}

Post a Comment

5 Comments

  1. I really enjoy the blog.Much thanks again. Really Great selenium training

    ReplyDelete
  2. You are doing a good job and sharing your knowledge to others! it was one of the pretty post to read and useful to improve the knowledge as updated one, keep doing the good work.
    Selenium Training in Electronic City

    ReplyDelete
  3. Hello, you used to write fantastic, but the last few posts have been kinda boring… I miss your tremendous writings. Past few posts are just a bit out of track! come on! design firms los angeles

    ReplyDelete
  4. Aw, it was a very good post. In concept I have to devote writing like this moreover – taking time and actual effort to make a great article… but what things can I say… I procrastinate alot through no means seem to get something carried out. los angeles web design

    ReplyDelete
  5. Pretty nice post. I simply stumbled upon your weblog and wanted to say that I have really enjoyed surfing around your blog posts. In any case I’ll be subscribing on your rss feed and I hope you write again soon! branding firms los angeles

    ReplyDelete
Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)