Java Small algorithm-count the number of occurrences of each word in a sentence __ algorithm

Source: Internet
Author: User
public class Demo2 {


public static void Main (string[] args) {
String words= "Look buddy, U got work hard and put yourself into your Java, Once you learned the heart of the Java, I can Gua Rantee that you win. ";

Regular match
String reg= "[a-za-z]+";
Pattern P=pattern.compile (REG);
Matcher M=p.matcher (words);

Storing a collection of words
Hashmap<string, integer> map=new hashmap<string, integer> ();
int count=0;//Words Total
while (M.find ()) {
count++;
String W=m.group ();
if (Null==map.get (W)) {//This word collection does not add a quantity of 1
Map.put (w, 1);
}else{//already added number +1
int X=map.get (w);
Map.put (w,x+1);
}
}
Total words
SYSTEM.OUT.PRINTLN ("Total words:" +count);
Traversal Collection Keyset
Set<string> Set=map.keyset ();
Iterator<string> I1=set.iterator ();
while (I1.hasnext ()) {
String Key=i1.next ();//key
Integer Value=map.get (key);//value
System.out.println ("Word:" +key+ "Occurrence times:" +value);
System.out.println ("------------------------------------------");
}
}

}


Results:

Total words: 26
Words: Look at the number of occurrences: 1
------------------------------------------
Word: Buddy Number of occurrences: 1
------------------------------------------
Word: Work number of occurrences: 1
------------------------------------------
Word: Heart Number of occurrences: 1
------------------------------------------
Word: Put occurrence number: 1
------------------------------------------
Word: Can occurrence number: 1
------------------------------------------
Word: Your number of occurrences: 1
------------------------------------------
Word: You appear number: 2
------------------------------------------
Word: Win number of occurrences: 1
------------------------------------------
Word: The number of occurrences: 2
------------------------------------------
Word: I Number of occurrences: 1
------------------------------------------
Word: In occurrences: 1
------------------------------------------
Words: and occurrences: 1
------------------------------------------
Word: U Number of occurrences: 1
------------------------------------------
Word: Number of occurrences: 1
------------------------------------------
Word: The number of occurrences: 1
------------------------------------------
Word: Once Number of occurrences: 1
------------------------------------------
Word: Learned number of occurrences: 1
------------------------------------------
Word: Guarantee Number of occurrences: 1
------------------------------------------
Word: Java occurrences: 2
------------------------------------------
Word: Yourself number of occurrences: 1
------------------------------------------
Word: Got number of occurrences: 1
------------------------------------------
Word: Hard number of occurrences: 1
------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.