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
------------------------------------------