Java 对文本文件中的字符串引用进行排序
我已将文件中的字符串存储到ArrayList中,并使用哈希集计算每个字符串的出现次数 我想列出前5个单词及其出现次数。我应该能够通过实现hashtable、treemap等来完成这项工作。我如何才能实现这一点 这是我的ArrayList:Java 对文本文件中的字符串引用进行排序,java,arraylist,hashset,Java,Arraylist,Hashset,我已将文件中的字符串存储到ArrayList中,并使用哈希集计算每个字符串的出现次数 我想列出前5个单词及其出现次数。我应该能够通过实现hashtable、treemap等来完成这项工作。我如何才能实现这一点 这是我的ArrayList: List<String> word_list = new ArrayList<String>(); while (INPUT_TEXT1.hasNext()) { String input_wo
List<String> word_list = new ArrayList<String>();
while (INPUT_TEXT1.hasNext()) {
String input_word = INPUT_TEXT1.next();
word_list.add(input_word);
}
INPUT_TEXT1.close();
int word_list_length = word_list.size();
System.out.println("There are " + word_list_length + " words in the .txt file");
System.out.println("\n\n");
System.out.println("word_list's elements are: ");
for (int i = 0; i<word_list.size(); i++) {
System.out.print(word_list.get(i) + " ");
}
System.out.println("\n\n");
List word_List=new ArrayList();
while(输入_TEXT1.hasNext()){
字符串输入\单词=输入\文本1.next();
单词列表。添加(输入单词);
}
输入_TEXT1.close();
int word_list_length=word_list.size();
System.out.println(“在.txt文件中有“+word\u list\u length+”个单词”);
System.out.println(“\n\n”);
System.out.println(“单词列表的元素是:”);
对于(int i=0;i您可以使用HashMap
(使用唯一的单词作为键和频率作为值进行保存),然后按照以下步骤中解释的相反顺序对值进行排序:
(1) 加载带有单词的单词列表
(2) 从word\u列表
(3) 将唯一单词存储到HashMap
中,唯一单词为key
,频率为value
(4) 使用值(频率)对哈希映射进行排序
您可以参考以下代码:
public static void main(String[] args) {
List<String> word_list = new ArrayList<>();
//Load your words to the word_list here
//Find the unique words now from list
String[] uniqueWords = word_list.stream().distinct().
toArray(size -> new String[size]);
Map<String, Integer> wordsMap = new HashMap<>();
int frequency = 0;
//Load the words to Map with each uniqueword as Key and frequency as Value
for (String uniqueWord : uniqueWords) {
frequency = Collections.frequency(word_list, uniqueWord);
System.out.println(uniqueWord+" occured "+frequency+" times");
wordsMap.put(uniqueWord, frequency);
}
//Now, Sort the words with the reverse order of frequency(value of HashMap)
Stream<Entry<String, Integer>> topWords = wordsMap.entrySet().stream().
sorted(Map.Entry.<String,Integer>comparingByValue().reversed()).limit(5);
//Now print the Top 5 words to console
System.out.println("Top 5 Words:::");
topWords.forEach(System.out::println);
}
publicstaticvoidmain(字符串[]args){
List word_List=new ArrayList();
//将您的单词加载到此处的单词列表中
//现在从列表中查找唯一的单词
String[]uniqueWords=word\u list.stream().distinct()。
toArray(大小->新字符串[大小];
Map wordsMap=newhashmap();
整数频率=0;
//加载要映射的单词,每个单词作为关键字,频率作为值
for(字符串uniqueWord:uniqueWords){
频率=集合频率(单词列表,uniqueWord);
System.out.println(uniqueWord+“发生”+频率+“次数”);
单词映射put(uniqueWord,frequency);
}
//现在,按频率的相反顺序对单词进行排序(HashMap的值)
Stream topWords=wordsMap.entrySet().Stream()。
排序(Map.Entry.comparingByValue().reversed()).limit(5);
//现在将前5个单词打印到console
System.out.println(“前5个单词:”);
topWords.forEach(System.out::println);
}
使用java 8并将所有代码放在一个块中
Stream<Map.Entry<String,Long>> topWords =
words.stream()
.map(String::toLowerCase)
.collect(groupingBy(identity(), counting()))
.entrySet().stream()
.sorted(Map.Entry.<String, Long> comparingByValue(reverseOrder())
.thenComparing(Map.Entry.comparingByKey()))
.limit(5);
创建一个内部类,比如Z,有两个字段——word、count,它实现了Compariable
并重写hashcode()
和equals()
方法。创建这个类的实例集——如果集合包含对象,则获取它并递增计数。使用集合对它进行排序。Sort()
。就这样。也就是说,Hashmap可能是更好的方法。在Apache Commons中有一个简单的实现,使用HashBag
Stream<Map.Entry<String,Long>> topWords =
words.stream()
.map(String::toLowerCase)
.collect(groupingBy(identity(), counting()))
.entrySet().stream()
.sorted(Map.Entry.<String, Long> comparingByValue(reverseOrder())
.thenComparing(Map.Entry.comparingByKey()))
.limit(5);
topWords.forEach(m -> {
System.out.print(m.getKey() + " : "+ m.getValue() + "time(s)");
});