java 文本文件中的 hashmap 字数统计
java hashmap word count from a text file
我正在尝试编写代码以从文本文件中读取信息,我需要找出以白色 space 分隔的每个单词出现了多少次。然后我需要按字母顺序输出每个单词的计数。我希望使用 TreeMap、keySet() 和迭代器。我的代码非常不完整,我很困惑。
import java.util.HashMap;
import java.util.Map
public class WordCount<E extends Comparable<E>> {
private static Map<String, Integer> map = new HashMap<String, Integer>();
static {
fillMap(map, "Alice.txt");
}
private static void fillMap(Map<String, Integer> map, String fileName) {
}
}
这正是您要求的代码。它将保存每个单词并计算它们。如果它得到的单词不存在,它将把它添加到 Map 中,如果存在,它会增加它的值。最后,它将打印所有键和值。
使用它,如果有任何问题,请提问。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/*
* @author Mr__Hamid
*/
public class overFlow {
public static void main(String[] args) throws FileNotFoundException, IOException {
Map m1 = new HashMap();
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String[] words = line.split(" ");//those are your words
for (int i = 0; i < words.length; i++) {
if (m1.get(words[i]) == null) {
m1.put(words[i], 1);
} else {
int newValue = Integer.valueOf(String.valueOf(m1.get(words[i])));
newValue++;
m1.put(words[i], newValue);
}
}
sb.append(System.lineSeparator());
line = br.readLine();
}
}
Map<String, String> sorted = new TreeMap<String, String>(m1);
for (Object key : sorted.keySet()) {
System.out.println("Word: " + key + "\tCounts: " + m1.get(key));
}
}
}
我正在尝试编写代码以从文本文件中读取信息,我需要找出以白色 space 分隔的每个单词出现了多少次。然后我需要按字母顺序输出每个单词的计数。我希望使用 TreeMap、keySet() 和迭代器。我的代码非常不完整,我很困惑。
import java.util.HashMap;
import java.util.Map
public class WordCount<E extends Comparable<E>> {
private static Map<String, Integer> map = new HashMap<String, Integer>();
static {
fillMap(map, "Alice.txt");
}
private static void fillMap(Map<String, Integer> map, String fileName) {
}
}
这正是您要求的代码。它将保存每个单词并计算它们。如果它得到的单词不存在,它将把它添加到 Map 中,如果存在,它会增加它的值。最后,它将打印所有键和值。 使用它,如果有任何问题,请提问。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
/*
* @author Mr__Hamid
*/
public class overFlow {
public static void main(String[] args) throws FileNotFoundException, IOException {
Map m1 = new HashMap();
try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String[] words = line.split(" ");//those are your words
for (int i = 0; i < words.length; i++) {
if (m1.get(words[i]) == null) {
m1.put(words[i], 1);
} else {
int newValue = Integer.valueOf(String.valueOf(m1.get(words[i])));
newValue++;
m1.put(words[i], newValue);
}
}
sb.append(System.lineSeparator());
line = br.readLine();
}
}
Map<String, String> sorted = new TreeMap<String, String>(m1);
for (Object key : sorted.keySet()) {
System.out.println("Word: " + key + "\tCounts: " + m1.get(key));
}
}
}