Created
December 18, 2018 11:20
-
-
Save sigursoft/cd8a9a0332f651d7181da6deb9720f55 to your computer and use it in GitHub Desktop.
10 most frequest words
This file contains hidden or 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
| import java.io.IOException; | |
| import java.nio.file.Files; | |
| import java.nio.file.Paths; | |
| import java.util.Arrays; | |
| import java.util.function.Function; | |
| import java.util.stream.Collectors; | |
| import java.util.stream.Stream; | |
| public class TopTen { | |
| public static void main(String[] args) { | |
| Arrays.stream(args) | |
| .flatMap(TopTen::fileLines) | |
| .flatMap(line -> Arrays.stream(line.split("\\b"))) | |
| .map(word -> word.replaceAll("[^a-zA-Z]", "")) | |
| .filter(word -> word.length() > 0) | |
| .map(word -> word.toLowerCase()) | |
| .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) | |
| .entrySet().stream() | |
| .sorted((a, b) -> -a.getValue().compareTo(b.getValue())) | |
| .limit(10) | |
| .forEach(e -> System.out.format("%s = %d%n", e.getKey(), e.getValue())); | |
| } | |
| private static Stream<String> fileLines(String path) { | |
| try { | |
| return Files.lines(Paths.get(path)); | |
| } catch (IOException e) { | |
| throw new RuntimeException(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment