java - 将地图放入其他地图的价值
问题描述
我想将 aMap<String, List<Integer>>
放入Map<Integer, Map<String, List<Integer>>>
.
try {
bufferedReader = new BufferedReader(new FileReader(new File(filePath)));
String currentLine;
int numLine=0;
while ((currentLine = bufferedReader.readLine()) != null) {
numLine++;
Collection<String> wordsInOneLigne = Arrays.asList(currentLine.split(" "));
List<String> distinctWordsInOneLigne = wordsInOneLigne.stream().distinct().collect(Collectors.toList());
for (String word : distinctWordsInOneLigne) {
if(!word.isEmpty()){
List<Integer> linePositionsOfWord = new LinkedList<>();
if(currentLine.contains(word)){
linePositionsOfWord.add(numLine);
map.clear();
map.put(filePath,linePositionsOfWord);
map1.put(word, map);
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
}
distinctWordsInOneLine 是一个包含行中单词的列表。
得到输出:{word1={D:\Files\file1.txt=[3]}, word3={D:\Files\file1.txt=[3]}, word2={D:\Files\file1.txt=[3]}}
预期输出:{word1={D:\Files\file1.txt=[numberoflinecontainsword]}, word3={D:\Files\file1.txt=[numberoflinecontainsword]}, word2={D:\Files\file1.txt=[numberoflinecontainsword]}}
我需要帮助:)
解决方案
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);
}
}
}
}
这项工作对我有好处!computeIfAbsent
现在我想通过使用and来改变优化它computeIfPresent
。
推荐阅读
- c# - 使用 ClaimsPrincial 从控制台连接到 SignalR 集线器
- javascript - 通过单击按钮使用 iframe 调整容器大小
- python - python selenium headless 运行方式与非无头版本不同
- c# - ASP.NET Core TextArea 不呈现值
- python - 如何创建一个接收图像并输出另一个图像的神经网络?
- python - SQLAlchemy 邻接列表 - 约束 parent_id 不等于 ID
- c - 添加到 typedef 结构内元素的索引
- python - 在 Python 中删除 CSV 的整个价格列中的最后 2 位数字,或者在最后两位数字之前添加一个小数?
- flutter - VSCode 上突出显示的错误
- python - 将 pandas 数据帧从 tsv 转换为 parquet