положить карту в значение другой карты - PullRequest
0 голосов
/ 27 апреля 2020

Я хочу поместить Map<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();
            }
        }

diverWordsInOneLine - список, содержащий слова в строке.

Получен вывод: {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]}}

Мне нужна помощь, пожалуйста:)

1 Ответ

0 голосов
/ 30 апреля 2020
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);
                                }
                            }
                        }
                    }

Это хорошо для меня! теперь я хочу изменить optimase с помощью computeIfAbsent и computeIfPresent.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...