I have very typical problem:
Given big text file, containing text. The task is to calculate occurence of each word and print this info from most occured to less.
cat cat cat dog dog mouse
cat -> 3 dog -> 2 mouse -> 1
First I solved this using
Scanner. But this solutions seems to be slow, cause IO was blocking.
After some time and cookie spent I wrote this solution:
final Path path = FileSystems.getDefault().getPath("/tmp/", "file.txt"); Map<String, Long> occ = Files .lines(path) .flatMap(line -> Arrays.stream(line.split(delimRegex))) .filter(line -> line.length() > 0) .map(String::toLowerCase) .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); occ .entrySet() .stream() .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue())) .forEach(e -> System.out.println(e.getKey() + " -> " + e.getValue()));
I think solution is good (but not sure). Is there any other optimization techniques I've ignored?