java - HashMap 和列表

标签 java list hashmap

我创建了一个函数,可以读取文本文件并使用 HashMap 计算单词的频率。然后我发现制作一个对 HashMap 进行排序的函数非常困难...所以经过一些研究,我发现了一些使用集合和列表对 HashMap 进行排序的代码。但是,该函数的输出是一个列表,而不是 HashMap 。一切正常并且完全符合我的要求。所以我的问题是,获取列表内容并将其放回到 HashMap 中的最有效方法是什么,以便它可以与我的代码的其余部分一起使用。

编辑

好吧,所以我很清楚这是无法实现的,因为它不是使用 HashMap 的目的。我问这个的唯一原因是因为我有现有的代码(在我必须实现更改之前)将其输出到文件。这在使用 HashMap 时有效,但现在它是一个列表,我有点困惑。

干杯

构建 HashMap

private static HashMap<String, Integer>  theHashMap(String inFileName) throws IOException {

    // Resets collections frequency values to zero
    for (Map.Entry<String, Integer> entry : collection.entrySet()) {
        entry.setValue(0);
    }

    // Reads in the new document file to an ArrayList
    Scanner textFile = new Scanner(new File(inFileName));
    ArrayList<String> file = new ArrayList<String>();

    while(textFile.hasNext()) {
        file.add(textFile.next().trim().toLowerCase());
    }

    for(String word : file) {
        Integer dict = collection.get(word);
        if (!collection.containsKey(word)) {
            collection.put(word, 1); 
        } else {
            collection.put(word, dict + 1);
        }
    }  

    textFile.close();  

    return collection;
}

对 HashMap 进行排序

private static List<Map.Entry<String, Integer>> sortTheHashMap(HashMap<String, Integer> values) {

    Set<Entry<String, Integer>> set = values.entrySet();
    List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
    Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
    {
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
        {
            return (o2.getValue()).compareTo(o1.getValue());
        }
    } );
    for(Map.Entry<String, Integer> entry:list){
        System.out.println(entry.getKey()+" = "+entry.getValue());
    }

    return list; 
}

输出到文件

    FileWriter fw;
    File fileName;

    fileName = new File("test.txt");
    fw = new FileWriter(fileName, true);

    for (String word : document.getKey()) {
        String key = word.toString();
        String value = document.get(word);
        fw.write(key + " " + value + "\n\n");
    }

    fw.close()

最佳答案

根据定义,Java HashMap 是未排序的。它明确地写在 Javadoc 中:

This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

如果您想要按键排序的 Map,请使用 TreeMap :

The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

但是,我不确定 map 是否真的是您想要的。映射用于通过键查找值。排序映射对键进行排序,看起来您想要对(出现次数)进行排序。如果有两个单词出现相同次数,它们应该在什么键下出现怎么办?

这就是为什么 Collections.sort() 返回一个列表 - 它对给定的集合进行排序并按照您想要的顺序放置元素。

关于java - HashMap 和列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51693668/

相关文章:

java - 初始化 VM 时出错 无法为对象堆保留足够的空间 无法创建 Java 虚拟机

java - 哈希桶的数量

java - Android Message 'what' 代码是否需要在处理程序或线程范围内是唯一的?

java - 如何通过java matcher正则表达式检查IP范围

java - 在java中根据另一个列表的值对多个列表进行排序

java - 在 for 循环期间从列表中删除对象

java - 如何在不使用 for 循环的情况下使用现有列表的成员变量创建 java 列表?

android - 如何使用 Proto DataStore 保存对象列表

java - 如何在java中合并两个嵌套的HashMap

javascript - 将对象数组转换为 HashMap ,由对象的属性值索引