java - 使用 ComputeIfAbsent 和 ComputeIfPresent 将列表放入映射中

标签 java dictionary

我有一组非常适合我的代码:

for (String word : distinctWordsInOneLigne) {
                        Map<String, List<Integer>> map = new HashMap<>();
                        if (!word.isEmpty()) {
                            List<Integer> linePositionsOfWord = new LinkedList<>();
                            if (currentLine.contains(word)) {
                                linePositionsOfWord.add(numLine);
                                if (mapAllWordsPositionInFilesInFolder.containsKey(word)) {
                                    Map<String, List<Integer>> mapList = mapAllWordsPositionInFilesInFolder.get(word);
                                    if (mapList.containsKey(filePath)) {
                                        List<Integer> list = mapList.get(filePath);
                                        list.add(numLine);
                                    } else {
                                        mapList.put(filePath, linePositionsOfWord);
                                    }
                                } else {
                                    map.put(filePath, linePositionsOfWord);
                                    mapAllWordsPositionInFilesInFolder.put(word, map);
                                }
                            }
                        }
                    }

注意:Map<String, Map<String, List<Integer>>> mapAllWordsPositionInFilesInFolder = new HashMap<>(); 我的结果是这样的:

{word1={file2.txt=[7], file1.txt=[1, 2]}, word2={file2.txt=[1, 2, 9, 13], file5.txt=[2, 3, 9]}}

现在我想要一些结果,但现在使用 ComputeIfAbsent & ComputeIfPresent而不是containsKey这一切if ... else .

我尝试过这个但不起作用:

mapAllWordsPositionInFilesInFolder.computeIfAbsent(word,v -> new HashMap<>())
                                        .computeIfAbsent(filePath,  val -> linePositionsOfWord);

mapAllWordsPositionInFilesInFolder.computeIfPresent(word,(k,v)->{
                                    v.computeIfPresent(filePath, (x, y) -> linePositionsOfWord.add(numLine));
                                    return v;
                                });

我需要帮助!谢谢:)

最佳答案

您不会为此使用 computeIfPresent(),但您可以像这样使用 computeIfAbsent():

for (String word : distinctWordsInOneLigne) {
    if (! word.isEmpty() && currentLine.contains(word)) {
        mapAllWordsPositionInFilesInFolder.computeIfAbsent(word, k -> new HashMap<>())
                                          .computeIfAbsent(filePath, k -> new LinkedList<>())
                                          .add(numLine);
    }
}
<小时/>

原来的代码写得很糟糕。即使不使用computeIfPresent(),也可以进行大量清理,消除重复的代码。 应该这样写:

for (String word : distinctWordsInOneLigne) {
    if (! word.isEmpty() && currentLine.contains(word)) {
        Map<String, List<Integer>> mapList = mapAllWordsPositionInFilesInFolder.get(word);
        if (mapList == null) {
            mapList = new HashMap<>();
            mapAllWordsPositionInFilesInFolder.put(word, mapList);
        }
        List<Integer> linePositionsOfWord = mapList.get(filePath);
        if (linePositionsOfWord == null) {
            linePositionsOfWord = new LinkedList<>();
            mapList.put(filePath, linePositionsOfWord);
        }
        linePositionsOfWord.add(numLine);
    }
}

通过内联,可以减少到:

for (String word : distinctWordsInOneLigne) {
    if (! word.isEmpty() && currentLine.contains(word)) {
        Map<String, List<Integer>> mapList = mapAllWordsPositionInFilesInFolder.get(word);
        if (mapList == null)
            mapAllWordsPositionInFilesInFolder.put(word, mapList = new HashMap<>());
        List<Integer> linePositionsOfWord = mapList.get(filePath);
        if (linePositionsOfWord == null)
            mapList.put(filePath, linePositionsOfWord = new LinkedList<>());
        linePositionsOfWord.add(numLine);
    }
}

关于java - 使用 ComputeIfAbsent 和 ComputeIfPresent 将列表放入映射中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61529126/

相关文章:

python - 当值是列表且项目不唯一时,交换字典键和值

python - 使用 {tuple :float} format 来自 dict 的 x 和 y 标签在 python matplotlib 中创建热图

python - 字符串索引必须是整数,而不是 str - Python 脚本

java - 强制显示 JLabel 子类

java - 无法使用Java初始化Spark上下文

java - 来自 matlab 的 HelloWorld.java

java - Mybatis foreach 迭代复杂对象参数中的整数列表

python - 如何在Python中获取dict列表而不是使用collection.defaultdict

javascript - 仅更改 JavaScript map 的一个实例

java - 请求参数在 Tomcat 中被丢弃