Skip to content

Instantly share code, notes, and snippets.

@ram0973
Created January 5, 2023 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ram0973/01397488b7a928ec49e17e260a9e4908 to your computer and use it in GitHub Desktop.
Save ram0973/01397488b7a928ec49e17e260a9e4908 to your computer and use it in GitHub Desktop.
task18.task1821
package org.example;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Solution {
public static void main(String[] args) throws IOException {
try (Stream<String> stream = Files.lines(Paths.get(args[0]), StandardCharsets.UTF_8)) {
stream
.flatMap(line -> Stream.of(line.split("")))
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.stream()
.sorted(Map.Entry.comparingByKey())
.forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue()));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment