I am trying to read from a file, count the frequency of each letter, and print the results. Uppercase letters should be treated as their lowercase equivalent. I have example code to start with which counts the frequency of different words in a file:
Pattern pattern = Pattern.compile("\\s+");
Map<String, Long> wordCounts =
Files.lines(Paths.get("myFile.txt"))
.flatMap(pattern::splitAsStream)
.collect(Collectors.groupingBy(String::toLowerCase, TreeMap::new, Collectors.counting()));
I am to modify this code to count letters instead. I believe most of my difficulty is in understanding what the object becomes as the code progresses and what I can do with the result. I know that Files.lines() outputs a Stream String where each String is actually one line in the file. From this answer, I know I could convert a String to a Stream Character with this code:
Stream<Character> sch = "abc".chars().mapToObj(i -> (char)i);
However, I can't quite figure out a way to use it to convert a Stream String.