Created
April 7, 2015 10:56
-
-
Save danielshaya/71aa92bb0bfe5e374ac3 to your computer and use it in GitHub Desktop.
Lambdas in one line
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
package util; | |
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