Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.time.Duration;
- import java.time.Instant;
- import java.util.Arrays;
- import java.util.Map;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- import static common.IO.println;
- import static java.util.Map.Entry;
- /**
- * A csv file of size 1.85 GB containing 10000000 rows of uuids each row having 5 comma separated uuid
- */
- final String inputPath = "/Users/amit.sen/Desktop/uuids-medium.csv";
- void main() {
- withStream();
- }
- void withStream() {
- try (var inputStream = Files.lines(Path.of(inputPath))) {
- var start = Instant.now();
- inputStream
- .parallel()
- .map(this::rowReduce)
- .flatMap(charMap -> charMap.entrySet().stream())
- .collect(Collectors.toMap(Entry::getKey, Entry::getValue, Long::sum))
- .entrySet()
- .forEach(System.out::println);
- println(STR."Time Taken : \{Duration.between(start, Instant.now()).getSeconds()} seconds");
- } catch (Exception ex) {
- System.err.println(ex.getMessage());
- }
- }
- Map<Character, Long> rowReduce(String row) {
- return Arrays.stream(row.split(","))
- .flatMap(uuid -> uuid.chars().mapToObj(i -> (char) i))
- .filter(ch -> ch != '-')
- .collect(Collectors.toMap(Function.identity(), _ -> 1L, Long::sum));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement