java - java中使用链表的快速字数统计
问题描述
所以这里是计算文本文档中出现次数最多的单词出现次数的程序。但是,由于我认为 get(i) 方法,它非常慢。任何想法如何使它更快?我知道如果我使用数组会更快,但我希望它保持为链表,如果可能的话,只需更改 get(i) 部分。
import java.io.File;
import java.util.Scanner;
import java.util.Map.Entry;
import java.util.AbstractMap;
import java.util.LinkedList;
public class wordcount {
public static Entry<String, Integer> count_LINKED_LIST(String[] tokens) {
LinkedList<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>();
for (int j = 0; j < tokens.length; j++) {
String word = tokens[j];
boolean found = false;
for (int i = 0; i < list.size(); i++) {
Entry<String, Integer> e = list.get(i);
if (word.equals(e.getKey())) {
e.setValue(e.getValue() + 1);
list.set(i, e);
found = true;
break;
}
}
if (!found)
list.add(new AbstractMap.SimpleEntry<String, Integer>(word, 1));
}
int maxCount = 0;
String maxWord = "";
for (int i = 0; i < list.size(); i++) {
int count = list.get(i).getValue();
if (count > maxCount) {
maxWord = list.get(i).getKey();
maxCount = count;
}
}
return new AbstractMap.SimpleEntry<String, Integer>(maxWord, maxCount);
}
static String[] readText(String PATH) throws Exception {
Scanner doc = new Scanner(new File(PATH)).useDelimiter("[^a-zA-Z]+");
int length = 0;
while (doc.hasNext()) {
doc.next();
length++;
}
String[] tokens = new String[length];
Scanner s = new Scanner(new File(PATH)).useDelimiter("[^a-zA-Z]+");
length = 0;
while (s.hasNext()) {
tokens[length] = s.next().toLowerCase();
length++;
}
doc.close();
return tokens;
}
public static void main(String[] args) throws Exception {
String PATH = "/Users/username/foldername/textdocument.txt";
String[] tokens = readText(PATH);
long startTime = System.currentTimeMillis();
Entry<String, Integer> entry = count_LINKED_LIST(tokens);
long endTime = System.currentTimeMillis();
String time = String.format("%12d", endTime - startTime);
System.out.println("time\t" + time + "\t" + entry.getKey() + ":" + entry.getValue());
}
}
解决方案
您可以为此使用映射(令牌是键,令牌出现是值)并使用 Java8+ Stream API:
public static Map<String, Long> count(String[] tokens) {
return Arrays.stream(tokens).collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
}
推荐阅读
- hibernate - 不将图像保存在数据库中,只想保存 url
- ruby-on-rails - Google App Engine 资产管道标准环境
- php - Nodemcu 不响应 http GET 请求
- pandas - 熊猫用数据框替换问题
- tsql - 如何在 azure cosmos db 中获取两个相同的属性
- c - AVR Atmega168 I2C LCD 不想初始化
- javascript - 如何将字符串拆分为特定字节大小的块?
- elasticsearch - NEST 不应用属性映射
- java - itext7 Java 为现有 pdf 中的标题创建 PdfExplicitDestination
- node.js - 如何将信息日志消息仅存储信息日志文件?