Created
April 10, 2015 12:50
-
-
Save mad4j/a91ea4aafb5944b7c463 to your computer and use it in GitHub Desktop.
find the 20 most frequent words in a file
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.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
/** | |
* Lambdas in one line | |
*/ | |
public class LambdaTest { | |
public static List<String> parse(Path path) throws Exception{ | |
return Files.lines(path) | |
.parallel() | |
.flatMap(line -> Arrays.asList(line.split("\\b")).stream()) | |
.collect(Collectors.groupingBy(w -> w, Collectors.counting())) | |
.entrySet().stream() | |
.sorted(Comparator.comparing(Map.Entry<String, Long>::getValue).reversed()) | |
.limit(20) | |
.map(Map.Entry::getKey) | |
.collect(Collectors.toList()); | |
} | |
public static void main(String... args) throws Exception{ | |
System.out.println(parse(Paths.get(args[0]))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment