信1705-2 软工作业最大重复词查询思路: (1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词)数组中。 (2)定义一个Map,key是字符串类型,保存单词;value是数字类型,保存该单词出现的次数。 (3)遍历(1)中得到的字符串数组,对于每一个单词,考察Map的key中是否出现过该单词,如果没出现过,map中增加一个元素,key为该单词,value为1(
通过学习学会了文本的访问,了解一点哈希表用途。经过网上查找做成了下面查询文章重复词的JAVA程序。
1 思
思路:
(1)将文章(一个字符串存储)按空格进行拆分(split)后,存储到一个字符串(单词)数组中。
(2)定义一个Map,getkey是字符串类型,保存单词;value是数字类型,保存该单词出现的次数。
(3)遍历(1)中得到的字符串数组,对于每一个单词,考察Map的getkey中是否出现过该单词,如果没出现过,map中增加一个元素,key为该单词,value为1(第一次出现);
如果,在map的getkey中发现了该单词,则通过key找到对应的value(单词出现的次数),将该value加1,再次保存回map。
(4)遍历(3)中得到的map,输出getkey(单词)及对应的value(次数)。
路
2 流程图
package LIULAN;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class en {
public static void Count(File file){
String str ="";
String result = "";
try {BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
while((str = bufferedReader.readLine())!=null){result = result+str;}bufferedReader.close();
}
catch (Exception e) {
}
System.out.println(result);
Map<String, Integer> map = new HashMap<String, Integer>();
Pattern p = Pattern.compile("[, . ; ! ? ]");
Matcher m = p.matcher(result);
String [] strs = p.split(result);
for(int i=0;i<strs.length;i++)
{ if(map.containsKey(strs[i])){int c = map.get(strs[i]);
c++;map.put(strs[i], c);
}else{map.put(strs[i], 1);
}
}
Set set = map.entrySet();
Iterator it = set.iterator();
int min = 100;
int max = 0;
String minWord = "";
String maxWord = "";
int x = 0;
while (it.hasNext()) {Entry<String, Integer> me = (Entry) it.next();
if((int) me.getValue()<min&&!((String) me.getKey()).equals("")){min = (int) me.getValue();
minWord = (String) me.getKey();
}
if((int) me.getValue()>=max&&!((String) me.getKey()).equals(""))
{
max = (int) me.getValue();
maxWord = (String) me.getKey();
}
System.out.println(me.getKey()+":"+me.getValue());
}System.out.println("出现次数最多的是"+":"+max+" "+maxWord);
}
private void println(Map map){Set set = map.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){Entry<String, Integer> entry = (Entry<String, Integer>) it.next();
String key = entry.getKey();
int value = entry.getValue();
}
}
public static void main(String[] args){
File file = new File("D:\\新建文件夹\\1\\KANWEN\\A.txt");
Count(file);}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class en {
public static void Count(File file){
String str ="";
String result = "";
try {BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
while((str = bufferedReader.readLine())!=null){result = result+str;}bufferedReader.close();
}
catch (Exception e) {
}
System.out.println(result);
Map<String, Integer> map = new HashMap<String, Integer>();
Pattern p = Pattern.compile("[, . ; ! ? ]");
Matcher m = p.matcher(result);
String [] strs = p.split(result);
for(int i=0;i<strs.length;i++)
{ if(map.containsKey(strs[i])){int c = map.get(strs[i]);
c++;map.put(strs[i], c);
}else{map.put(strs[i], 1);
}
}
Set set = map.entrySet();
Iterator it = set.iterator();
int min = 100;
int max = 0;
String minWord = "";
String maxWord = "";
int x = 0;
while (it.hasNext()) {Entry<String, Integer> me = (Entry) it.next();
if((int) me.getValue()<min&&!((String) me.getKey()).equals("")){min = (int) me.getValue();
minWord = (String) me.getKey();
}
if((int) me.getValue()>=max&&!((String) me.getKey()).equals(""))
{
max = (int) me.getValue();
maxWord = (String) me.getKey();
}
System.out.println(me.getKey()+":"+me.getValue());
}System.out.println("出现次数最多的是"+":"+max+" "+maxWord);
}
private void println(Map map){Set set = map.entrySet();
Iterator it = set.iterator();
while(it.hasNext()){Entry<String, Integer> entry = (Entry<String, Integer>) it.next();
String key = entry.getKey();
int value = entry.getValue();
}
}
public static void main(String[] args){
File file = new File("D:\\新建文件夹\\1\\KANWEN\\A.txt");
Count(file);}
}
![](https://img2018.cnblogs.com/blog/1279412/201810/1279412-20181011181420790-74504966.png)
![](https://img2018.cnblogs.com/blog/1279412/201810/1279412-20181011181500838-1736250889.png)