Tuesday 15 January 2013

Word Count & Occurance In Paragraph Using HashMap and Hashset



package com.string.manipulation;
 
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
public class WordCount {
       public static void main(String[] args) {
            String str = "Java Code Code Geeks (JCGs) is an independent online community focused on creating the ultimate Java-to-Java developers resource center; targeted at the technical architect, technical team lead (senior developer), project manager and junior developers alike. JCGs serve the Java, Scala, Android, SOA, Agile and Telecom communities with daily news written by domain experts, articles, tutorials, reviews, announcements, code snippets and open source projects";
            String replaceStr[] = str.split(" ");
             HashSet hashSet = new HashSet();
            HashMap hashMap = new HashMap();
            for (int i = 0; i < replaceStr.length; i++) {
                    if (hashSet.contains(replaceStr[i])) {
                         int cnt = Integer.parseInt(hashMap.get(replaceStr[i])
                                                                        .toString());
                                 ++cnt;
                                 hashMap.put(replaceStr[i], String.valueOf(cnt));
                         } else {
                                  hashSet.add(replaceStr[i]);
                                  hashMap.put(replaceStr[i], "1");
                         }
              }
             Iterator iterator = hashMap.entrySet().iterator();
             while (iterator.hasNext()) {
                   Map.Entry pairs = (Map.Entry) iterator.next();
                   System.out.println(pairs.getKey() + " = " + pairs.getValue());
              }
             System.out.println("  replaceStr.length " + replaceStr.length);
          }
}


 

 

OutPut

manager = 1
Agile = 1
communities = 1
lead = 1
by = 1
technical = 2
ultimate = 1
Java = 1
Telecom = 1
open = 1
focused = 1
(JCGs) = 1
on = 1
serve = 1
creating = 1
code = 1
team = 1
targeted = 1
and = 3
alike. = 1
junior = 1
projects = 1
project = 1
announcements, = 1
Java-to-Java = 1
snippets = 1
(senior = 1
Scala, = 1
SOA, = 1
tutorials, = 1
Code = 2
online = 1
architect, = 1
Android, = 1
written = 1
domain = 1
JCGs = 1
Java, = 1
Geeks = 1
is = 1
with = 1
news = 1
at = 1
resource = 1
community = 1
experts, = 1
articles, = 1
the = 3
developer), = 1
developers = 2
daily = 1
independent = 1
center; = 1
source = 1
an = 1
reviews, = 1
 
 

replaceStr.length 63