java - 对文本文件中出现的字符串进行排序

标签 java arraylist hashset

我已将文件中的字符串存储到 ArrayList 中,并使用 HashSet 来计算每个字符串出现的次数。

我希望列出前 5 个单词及其出现次数。我应该能够在不实现哈希表、 TreeMap 等的情况下完成此任务。我该如何实现此目标?

这是我的数组列表:

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");

这是我的哈希集:

Set<String> unique_word = new HashSet<String>(word_list);

    int number_of_unique = unique_word.size();

    System.out.println("unique worlds are: ");

    for (String e : unique_word) {
        System.out.print(e + " ");

    }

    System.out.println("\n\n");


    String [] word = new String[number_of_unique];
    int [] freq = new int[number_of_unique];

    int count = 0;

    System.out.println("Frequency counts : ");

    for (String e : unique_word) {
        word[count] = e;
        freq[count] = Collections.frequency(word_list, e);



        System.out.println(word[count] + " : "+ freq[count] + " time(s)");
        count++;

    }

难道是我多虑了一步?提前致谢

最佳答案

您可以使用HashMap(将唯一单词作为,将频率作为)来完成此操作,然后对值进行排序 按照以下步骤所述的相反顺序:

(1) 加载包含单词的 word_list

(2) 从word_list中查找唯一的单词

(3) 将唯一词存储到HashMap中,以唯一词为key,频率为value

(4) 按值(频率)对 HashMap 进行排序

您可以引用以下代码:

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);
 }

关于java - 对文本文件中出现的字符串进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40700753/

相关文章:

java - 数组不存储数据库中的对象

java - 将 ArrayList 添加到类中 - 它有什么作用?

java - HashSet 中元素的顺序是如何工作的?

java - 匹配句子java中列表/数组的任何单词

java - android中的自定义复选框难度

java - ArrayList 中的 concurrentModificationException

java - 在处理具有 1 亿个元素的 ArrayList 时提高速度和内存消耗

java - 重复的自定义对象被添加到哈希集中

java - 如何创建一个根据调用者类型进行过滤的切入点?

C# 将数组列表保存到文件?