Skip to content

Instantly share code, notes, and snippets.

@mad4j
Created April 10, 2015 12:50
Show Gist options
  • Save mad4j/a91ea4aafb5944b7c463 to your computer and use it in GitHub Desktop.
Save mad4j/a91ea4aafb5944b7c463 to your computer and use it in GitHub Desktop.
find the 20 most frequent words in a file
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