Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何排序并生成HashMap_Java_Collections_Nested_Hashset_Inverted Index - Fatal编程技术网

Java 如何排序并生成HashMap

Java 如何排序并生成HashMap,java,collections,nested,hashset,inverted-index,Java,Collections,Nested,Hashset,Inverted Index,我正在用Java构建一个反向索引。我已经用读取的文件中的数据填充了结构。现在我想对结构的所有三个级别进行排序,然后创建一个不可修改的副本,以返回到另一个类,以便将其写入文件。理想情况下,我可以使用Collections.sort和Collections.unmodifiableSet/Collections.unmodifiableMap函数。不,我不想用树集 当前代码: public HashMap<String, HashMap<Path, HashSet<Integer&

我正在用Java构建一个反向索引。我已经用读取的文件中的数据填充了结构。现在我想对结构的所有三个级别进行排序,然后创建一个不可修改的副本,以返回到另一个类,以便将其写入文件。理想情况下,我可以使用Collections.sort和Collections.unmodifiableSet/Collections.unmodifiableMap函数。不,我不想用树集

当前代码:

public HashMap<String, HashMap<Path, HashSet<Integer>>> getInvertedIndex()
{

    // sortInvertedIndex();
    HashMap<String, HashMap<Path, HashSet<Integer>>> copyOfInvertedIndex = null;

    // Set<String> words = invertedIndex.keySet();
    Set<Entry<String, HashMap<Path, HashSet<Integer>>>> m = invertedIndex.entrySet();

    // copyOfInvertedIndex = Collections.unmodifiableMap(invertedIndex);
    return copyOfInvertedIndex;
}

public void sortInvertedIndex()
{

    // List words = new List();
    // Collections.addAll(words, invertedIndex.keySet());
    // invertedIndex.keySet();
    // // SortedSet<String> keys = new TreeSet<String>(myHashMap.keySet());
    // // SortedSet<String> values = new
    // TreeSet<String>(myHashMap.values());
    // Collections.sort(list);

}

这个太宽了。您要排序什么?您想如何排序?我想对所有三个级别进行排序。对于HashMap的第一级,按字母顺序排列。对于HashMap的第二级,我想按字母顺序对if进行排序。我可以把它转换成字符串。上面的字符串单词用于保存文件名。对于HashSet的第三级,我希望它从最小到最大排序。希望这能澄清!你叫什么?HashMap不应保留其键或值的顺序。返回已排序的哈希映射没有意义。@tcsiwula为什么不使用TreeMap或TreeSet?只能通过Collection::sort对列表进行排序。您还应该考虑使用接口作为返回值和变量类型。您应该隐藏实现细节。