/
EugenyCh7
/
JavaAndScalaLessons
Обзор
Документация
Войти
/
EugenyCh7
/
JavaAndScalaLessons
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
JavaTest/src/ch9/ex2/Main.java
41 строка
2 KB
Eugeny Chekmarev
IDEA update
15 июн 2021, 21:11
15 июн 2021, 21:11
47fadf9
Код
Авторство
О чём код?
package ch9.ex2; import java.io.*; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.*; import java.util.stream.Collectors; public class Main { public static void main(String[] args) throws IOException { List<String> lines = Files.readAllLines(Paths.get("The Little Prince.txt"), StandardCharsets.UTF_8); Map<String, List<Integer>> wordsDict = new HashMap<>(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); Map<String, Long> map = Arrays.stream(line.split("[^\\pL']+")) .filter(s -> s.replaceAll("'", "").length() > 0) .collect(Collectors.groupingBy(String::toLowerCase, Collectors.counting())); int n = i + 1; for (String word : map.keySet()) { if (wordsDict.containsKey(word)) { wordsDict.get(word).add(n); } else { wordsDict.put(word, new ArrayList<>() {{ add(n); }}); } } } try (OutputStream outStream = Files.newOutputStream(Paths.get("The Little Prince.toc"))) { PrintWriter out = new PrintWriter(outStream, true, StandardCharsets.UTF_8); for (Map.Entry<String, List<Integer>> entry : wordsDict.entrySet()) { out.printf("%s", entry.getKey()); for (Integer lineNumber : entry.getValue()) { out.printf(" %d", lineNumber); } out.println(); } } } }