JAVA程序的搜索和排序问题
发布网友
发布时间:2022-04-23 03:17
我来回答
共1个回答
热心网友
时间:2023-08-14 14:00
package com.wjy.test;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
public class SearchAndSort {
private InputStream in = null;
private String filePath = "C:\\杂\\test.txt";
public static void main(String[] args) {
SearchAndSort sas = new SearchAndSort();
String[] keyword = {"等待","公司", "数据"}; // 关键词
String[] weightWords = {"数据"}; // 权重词
Map<String, String> map = new HashMap<String, String>(); // 结果集
String fileContent = sas.readFile();
for (String word : keyword) { // 处理关键词,并将结果插入结果集
map = sas.countWord(fileContent, word, map);
}
// 通过ArrayList构造函数把map.entrySet()转换成list
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
// 通过比较器实现比较排序
Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
public int compare(Map.Entry<String, String> mapping1, Map.Entry<String, String> mapping2) {
return mapping1.getValue().compareTo(mapping2.getValue());
}
});
Iterator<String> keys = map.keySet().iterator();
while(keys.hasNext()) { // 迭代打印结果集
String key = keys.next();
String value = map.get(key);
for (String str : weightWords) { // 比较关键词是否为权重词
if (str.equals(key)) { // 权重词在后面加X
value = value + "X";
break;
}
}
System.out.println(key + "出现" + value + "次");
}
}
/**
*
* 读取文件返回文件内容
*
* @return
*/
public String readFile() {
try {
in = new FileInputStream(filePath);
InputStreamReader isr = new InputStreamReader(in, Charset.forName("GBK"));
BufferedReader br = new BufferedReader(isr);
StringBuffer sb = new StringBuffer();
String line = " ";
// 利用StringBuffer将文件内容读成一行
while ((line = br.readLine()) != null){
sb.append(line);
}
// String content = sb.toString();
// 因为有部分内容因为换行导致原本在一起的内容分开,所以这里将字符串中的回车、换行等去掉
String content = Pattern.compile("\t|\r|\n").matcher(sb.toString()).replaceAll("");
//System.out.println(content);
return content;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
return null;
}
/**
*
* 统计关键词在文件内容中出现的次数
*
* @param fileContent
* @param word
* @param map
* @return
*/
public Map<String, String> countWord(String fileContent, String word, Map<String, String> map) {
int count = 0;
int length = word.length();
String content = fileContent;
int index = content.indexOf(word);
while (index != -1) { // 计算关键词出现次数
index = content.indexOf(word);
if (index == -1) {
break;
}
// 截取掉当前取到的关键词
content = content.substring(index + length);
count++;
}
map.put(word, count + "");
return map;
}
}