-
-
Save gakuzzzz/0acc993f0ee502fcf5d4 to your computer and use it in GitHub Desktop.
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.Map.Entry; | |
import java.util.Map; | |
import java.util.stream.Stream; | |
import static java.util.Comparator.comparing; | |
import static java.util.stream.Collectors.groupingBy; | |
import static java.util.stream.Collectors.counting; | |
public class Hoge { | |
public static void main(String[] args) throws IOException { | |
final Map<String, Long> counts; | |
try (final Stream<String> lines = Files.lines(Paths.get(args[0]))) { | |
counts = lines.flatMap(line -> Arrays.stream(line.split("\\W+"))) | |
.filter(word -> !word.isEmpty()) | |
.collect(groupingBy(a -> a, counting())); | |
} | |
counts.entrySet() | |
.stream().sorted(comparing(Entry::getValue)) | |
.map(it -> it.getValue() + ":" + it.getKey()) | |
.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment