有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用ComputeFabSent和ComputeiPresent将列表放入映射

我有一堆代码对我来说很好:

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而不是{}和所有这些{}

我试过了,但没有成功:

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

我需要帮助!谢谢:)


共 (1) 个答案

  1. # 1 楼答案

    你不会用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);
        }
    }