Java 将映射放入其他映射的值中

Java 将映射放入其他映射的值中,java,dictionary,hashmap,put,Java,Dictionary,Hashmap,Put,我想把一个Map放入Map的值中 试试看{ bufferedReader=new bufferedReader(新文件读取器(新文件(文件路径)); 串电流线; int numLine=0; 而((currentLine=bufferedReader.readLine())!=null){ numLine++; 集合词sinoneligne=Arrays.asList(currentLine.split(“”); List distinctWordsInOneLigne=wordsInOneLi

我想把一个
Map
放入
Map
的值中

试试看{
bufferedReader=new bufferedReader(新文件读取器(新文件(文件路径));
串电流线;
int numLine=0;
而((currentLine=bufferedReader.readLine())!=null){
numLine++;
集合词sinoneligne=Arrays.asList(currentLine.split(“”);
List distinctWordsInOneLigne=wordsInOneLigne.stream().distinct().collect(Collectors.toList());
for(字符串字:distinctwordinoneligne){
如果(!word.isEmpty()){
List linePositionsOfWord=新建LinkedList();
if(currentLine.contains(word)){
linePositionsOfWord.add(numLine);
map.clear();
map.put(文件路径、行位置);
放(字,图);
}
}
}
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
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=[numberoflinecontainsSwarm]},word3={D:\Files\file1.txt=[numberoflinecontainsSwarm]},word2={D:\Files\file1.txt=[numberoflinecontainsSwarm]}

我需要帮助:)

(字符串字:distinctwordinoneligne){
Map Map=newhashmap();
如果(!word.isEmpty()){
List linePositionsOfWord=新建LinkedList();
if(currentLine.contains(word)){
linePositionsOfWord.add(numLine);
if(mapallWordsPositionInflesInfolder.containsKey(word)){
MapMapList=mapAllWordsPositionInFilesInFolder.get(word);
if(mapList.containsKey(文件路径)){
List=mapList.get(文件路径);
列表。添加(numLine);
}否则{
mapList.put(文件路径、行位置);
}
}否则{
map.put(文件路径、行位置);
mapAllWordsPositionInFilesInFolder.put(word,map);
}
}
}
}

这工作对我有好处!现在我想通过使用
computeifassent
computeIfPresent

来更改优化它,但我不确定您在这里想做什么,但是
map.clear()
非常可疑-在循环的每次迭代中,它始终是同一个对象。因此,如果您在第10次迭代中清除它,那么之前所有迭代中的对象也将被清除。也许你应该在while循环的每次迭代中创建一个新的映射或者类似的东西。即使我删除了这一行,也没有任何变化
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();
            }
        }
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);
                                }
                            }
                        }
                    }