(1) Write a program that parses the frequency of occurrences of each word in a string and displays the word and the frequency at which it appears. (the words are separated by a space, such as "Hello World My First Unit Test");
Filewoedcount:
Import Java.io.BufferedReader;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import java.util.*;
public class Filewordcount {
public static void Main (string[] args) {
try {
BufferedReader br = new BufferedReader (New FileReader ("D:\\test.txt"));
String s;
StringBuffer sb = new StringBuffer ();
while ((s = br.readline ()) = null) {
Sb.append (s);
}
map<string,integer> map = new hashmap<string, integer> ();
StringTokenizer st = new StringTokenizer (sb.tostring (), ",.! \ n");
while (St.hasmoretokens ()) {
String letter = St.nexttoken ();
int count;
if (map.get (letter) = = null) {
Count = 1;
} else {
Count = Map.get (letter). Intvalue () + 1;
}
Map.put (Letter,count);
}
set<wordentity> set = new treeset<wordentity> ();
For (String Key:map.keySet ()) {
Set.add (New Wordentity (Key,map.get (key));
}
Stitching strings yourself, outputting the desired string format
System.out.println ("Output form one:");
for (iterator<wordentity> it = Set.iterator (); It.hasnext ();) {
wordentity w = it.next ();
System.out.println ("Word:" + w.getkey () + "occurrences:" + w.getcount ());
}
Print the Wordentity object directly, implement the output effect we want, just rewrite the ToString () method in the Wordentity class
System.out.println ("Output form II:");
for (iterator<wordentity> it = Set.iterator (); It.hasnext ();) {
wordentity w = it.next ();
System.out.println (w);
}
We can control only the top three of the output.
System.out.println ("Output Form III:");
int count = 1;
for (iterator<wordentity> it = Set.iterator (); It.hasnext ();) {
wordentity w = it.next ();
System.out.println ("+ count +" named Word: "+ w.getkey () +" occurs as many times as: "
+ W.getcount ());
if (count = = 3)//When output 3 out of loop
Break
count++;
}
} catch (FileNotFoundException e) {
SYSTEM.OUT.PRINTLN ("File not found! ");
} catch (IOException e) {
SYSTEM.OUT.PRINTLN ("file read abnormal!") ");
}
}
}
Filewordcounttest:
Import Org.junit.After;
Import Org.junit.AfterClass;
Import Org.junit.Before;
Import Org.junit.BeforeClass;
public class Filewordcounttest {
@BeforeClass
public static void Setupbeforeclass () throws Exception {
}
@AfterClass
public static void Teardownafterclass () throws Exception {
}
@Before
public void SetUp () throws Exception {
}
@After
public void TearDown () throws Exception {
}
}
Wordentity:
public class Wordentity implements comparable<wordentity> {
Private String key;
Private Integer count;
Public wordentity (String key, Integer count) {
This.key = key;
This.count = count;
}
public int compareTo (wordentity o) {
int cmp = Count.intvalue ()-O.count.intvalue ();
return (CMP = = 0 Key.compareto (o.key):-CMP);
Just add a minus sign here to decide whether to sort ascending or descending-cmp in descending order, CMP ascending
Because TreeSet will call Workformap's CompareTo method to determine its own sort
}
@Override
Public String toString () {
Return key + "occurrences are:" + count;
}
Public String GetKey () {
Return key;
}
Public Integer GetCount () {
return count;
}
}
Software Test Experiment Two