首页 > 解决方案 > 将地图放入其他地图的价值

问题描述

我想将 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]}}

我需要帮助:)

标签: javadictionaryhashmapput

解决方案


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


推荐阅读