Skip to content

Instantly share code, notes, and snippets.

@kh0ma
Last active July 17, 2018 19:07
Show Gist options
  • Save kh0ma/508a2842b9b9c3dd1f012ecbb3c73aeb to your computer and use it in GitHub Desktop.
Save kh0ma/508a2842b9b9c3dd1f012ecbb3c73aeb to your computer and use it in GitHub Desktop.
Count words in file
package com.kh0ma.demo;
import java.io.IOException;
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.Map;
import java.util.stream.Collectors;
/**
* @author Oleksandr Khomenko
*/
public class UniqueWords {
public static final String WHITESPACE_REGEX = "\\s+";
public static final String NON_WORD_REGEX = "[^\\w]";
public static final String BLANK_STRING = "";
public static void main(String[] args) throws IOException {
Map<String, Integer> result = countUniqueWordsInFile(Paths.get(args[0]));
result.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry::getValue))
.forEach(System.out::println);
}
private static Map<String, Integer> countUniqueWordsInFile(Path pathToFile) throws IOException {
return Files.lines(pathToFile)
.map(line -> line.split(WHITESPACE_REGEX))
.flatMap(Arrays::stream)
.map(w -> w.replaceAll(NON_WORD_REGEX, BLANK_STRING))
.filter(s -> !s.isEmpty())
.collect(Collectors.toMap(k -> k, v -> 1, (oldVal, newVal) -> oldVal + newVal));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment