gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:59:38 28 4
gpt4 key购买 nike

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

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com