Created
April 22, 2021 08:59
-
-
Save EugenyB/55b4cda3c84ca859318d706198724834 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Exercise 11: Gather the words into a map, accumulating a count of the | |
// number of occurrences of each word. Don't worry about upper case and | |
// lower case. Extra challenge: implement two solutions, one that uses | |
// groupingBy() and the other that uses toMap(). | |
@Test | |
public void wordFrequencies() throws IOException { | |
// Map<String, Long> map = reader.lines() | |
// .flatMap(s -> Arrays.stream(s.split(REGEXP))) | |
// .filter(s -> !s.isEmpty()) | |
// .collect(Collectors.groupingBy(s->s, Collectors.counting())); | |
Map<String, Long> map = reader.lines() | |
.flatMap(s -> Arrays.stream(s.split(REGEXP))) | |
.filter(s -> !s.isEmpty()) | |
.collect(Collectors.toMap(s->s, s->1L, Long::sum)); | |
assertEquals(2L, (long)map.get("tender")); | |
assertEquals(6L, (long)map.get("the")); | |
assertEquals(1L, (long)map.get("churl")); | |
assertEquals(2L, (long)map.get("thine")); | |
assertEquals(3L, (long)map.get("world")); | |
assertFalse(map.containsKey("lambda")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment