-
-
Save chrisseaton/669c85e7254da47220c62e1c4074f92e 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
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