Skip to content

Instantly share code, notes, and snippets.

@gakuzzzz
Forked from tokuhirom/Hoge.java
Last active August 29, 2015 14:19
Show Gist options
  • Save gakuzzzz/0acc993f0ee502fcf5d4 to your computer and use it in GitHub Desktop.
Save gakuzzzz/0acc993f0ee502fcf5d4 to your computer and use it in GitHub Desktop.
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