题目
利用一个java小程序对文本内的单词进行计数,有以下要求:
1.以字母序进行输出
2.以小写形式输出
输入:
The quick brown fox
Hopefully245this—is a quick13947
task&&#%*for you to complete.
But maybe the tASk 098234 will be less
…quicK.
the the the the the the the the the the
输出:
1 a
1 be
1 brown
1 but
1 complete
1 for
1 fox
1 hopefully
1 is
1 less
1 maybe
3 quick
2 task
12 the
1 this
1 to
1 will
1 you
解法
程序可分为几个部分,文本读入,单词处理,以及计数输出。文本读入利用普通输入流来完成,单词处理使用正则表达式对文本进行处理,之后替代,计数输出将表进行排序然后构建哈希表进行输出,值得学习的是哈希表的输出方式,在输出过程中用到了iterator和Map.entry。另外值得标记一下的是,HashMap是无序的,LinkedHashMap才是有序的,不过在本题情况下使用TreeMap貌似是个更好的选择
实现代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.NoSuchFileException;
import java.util.*;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test{
public static void main(String[] args) throws IOException{
ArrayList<String> AL = new ArrayList<String>();
try {
FileInputStream IS = new FileInputStream("workshop1.txt");
Scanner S = new Scanner(IS);
while(S.hasNextLine()){
StringTokenizer st = new StringTokenizer(StringFunc(S.nextLine()));
while(st.hasMoreTokens()) {
AL.add(st.nextToken());
}
}
IS.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashFunc(AL);
}
//handle the string
public static String StringFunc(String Str) {
Str = Str.toLowerCase();
Str = Pattern.compile("[^A-Za-z]+").matcher(Str).replaceAll(" ");
return Str;
}
//put elements in a hashtable and count how many times they appear
public static void HashFunc(ArrayList<String> AL) {
HashMap<String, Integer> Hmap = new LinkedHashMap<>();
Collections.sort(AL);
for (String temp : AL) {
Integer count = Hmap.get(temp);
Hmap.put(temp, (count == null) ? 1 : count + 1);
}
Iterator iter = Hmap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println(val + " " + key);
}
}
}
更新一下,利用TreeMap实现上述功能,代码如下:
public class Test{
public static void main(String[] args) throws IOException{
TreeMap<String, Integer> Hmap = new TreeMap<String, Integer>();
try {
FileInputStream IS = new FileInputStream("workshop1.txt");
Scanner S = new Scanner(IS);
while(S.hasNextLine()){
StringTokenizer st = new StringTokenizer(StringFunc(S.nextLine()));
while(st.hasMoreTokens()) {
String temp = st.nextToken();
Integer count = Hmap.get(temp);
Hmap.put(temp, (count == null) ? 1 : count + 1);
}
}
IS.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HashFunc(Hmap);
}
//handle the string
public static String StringFunc(String Str) {
Str = Str.toLowerCase();
Str = Pattern.compile("[^A-Za-z]+").matcher(Str).replaceAll(" ");
return Str;
}
//put elements in a hashtable and count how many times they appear
public static void HashFunc(TreeMap<String, Integer> Hmap) {
Iterator iter = Hmap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
System.out.println(val + " " + key);
}
}
}