Last active
July 17, 2018 19:07
-
-
Save kh0ma/508a2842b9b9c3dd1f012ecbb3c73aeb to your computer and use it in GitHub Desktop.
Count words in file
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 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