最常出现的词显示
Most frequent word display
我有一个问题,因为我每次评价只拿60分,如果我有两个单词相同的次数,我不知道如何显示字典最小值。
我实际上必须显示文本中最常见的单词,如果有两个单词出现次数相同以显示词典最小值。
我真的不知道如何使用 HashMap 来做到这一点,修复?
这是我的代码!
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Map < String, Integer > map = new LinkedHashMap < String, Integer > ();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(System.in));
String currentLine = reader.readLine();
while (currentLine != null) {
String[] input = currentLine.replaceAll("[^a-zA-Z]", " ").toLowerCase().split(" ");
currentLine.replace('\n', ' ');
for (int i = 0; i < input.length; i++) {
if (map.containsKey(input[i])) {
int count = map.get(input[i]);
map.put(input[i], count + 1);
} else {
map.put(input[i], 1);
}
}
currentLine = reader.readLine();
}
String mostRepeatedWord = null;
int count = 0;
for (Map.Entry < String, Integer > m: map.entrySet()) {
if (m.getValue() > count) {
mostRepeatedWord = m.getKey();
count = m.getValue();
}
else if (m.getValue() == count) {
mostRepeatedWord = m.getKey();
count = m.getValue();
if ()
}
}
System.out.println(mostRepeatedWord);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
您还可以将 mostRepeatedWord
与当前键进行比较,以防循环本身出现平局,如下所示:
String mostRepeatedWord = null;
int count = 0;
for (Map.Entry < String, Integer > m: map.entrySet()) {
if (m.getValue() > count) {
mostRepeatedWord = m.getKey();
count = m.getValue();
} else if (m.getValue() == count) {
String key = m.getKey();
if ( key.compareTo(mostRepeatedWord) ) < 0 {
mostRepeatedWord = key;
}
}
}
我有一个问题,因为我每次评价只拿60分,如果我有两个单词相同的次数,我不知道如何显示字典最小值。 我实际上必须显示文本中最常见的单词,如果有两个单词出现次数相同以显示词典最小值。 我真的不知道如何使用 HashMap 来做到这一点,修复? 这是我的代码!
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Map < String, Integer > map = new LinkedHashMap < String, Integer > ();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(System.in));
String currentLine = reader.readLine();
while (currentLine != null) {
String[] input = currentLine.replaceAll("[^a-zA-Z]", " ").toLowerCase().split(" ");
currentLine.replace('\n', ' ');
for (int i = 0; i < input.length; i++) {
if (map.containsKey(input[i])) {
int count = map.get(input[i]);
map.put(input[i], count + 1);
} else {
map.put(input[i], 1);
}
}
currentLine = reader.readLine();
}
String mostRepeatedWord = null;
int count = 0;
for (Map.Entry < String, Integer > m: map.entrySet()) {
if (m.getValue() > count) {
mostRepeatedWord = m.getKey();
count = m.getValue();
}
else if (m.getValue() == count) {
mostRepeatedWord = m.getKey();
count = m.getValue();
if ()
}
}
System.out.println(mostRepeatedWord);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
您还可以将 mostRepeatedWord
与当前键进行比较,以防循环本身出现平局,如下所示:
String mostRepeatedWord = null;
int count = 0;
for (Map.Entry < String, Integer > m: map.entrySet()) {
if (m.getValue() > count) {
mostRepeatedWord = m.getKey();
count = m.getValue();
} else if (m.getValue() == count) {
String key = m.getKey();
if ( key.compareTo(mostRepeatedWord) ) < 0 {
mostRepeatedWord = key;
}
}
}