Advertisement
amitsen01ei

ReadCsv.java

Feb 17th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | Source Code | 0 0
  1. import java.nio.file.*;
  2. import java.time.*;
  3. import java.util.*;
  4. import java.util.function.Function;
  5. import java.util.stream.Collectors;
  6.  
  7. import static common.io.Commons.println;
  8. import static java.util.Map.Entry;
  9.  
  10. /**
  11.  * A csv file of size 1.85 GB containing 10000000 rows of uuids each row having 5 comma separated uuid
  12.  */
  13. final String inputPath = "/Users/amitsen01ei/Downloads/uuids-medium.csv";
  14.  
  15. void main() {
  16.     withStream();
  17. }
  18.  
  19. void withStream() {
  20.     try (var inputStream = Files.lines(Path.of(inputPath))) {
  21.  
  22.         var start = Instant.now();
  23.  
  24.         inputStream
  25.                 .map(this::rowReduce)
  26.                 .flatMap(charMap -> charMap.entrySet().stream())
  27.                 .collect(Collectors.toMap(Entry::getKey, Entry::getValue, Long::sum))
  28.                 .entrySet()
  29.                 .forEach(System.out::println);
  30.  
  31.         println(STR."Time Taken : \{Duration.between(start, Instant.now()).getSeconds()} seconds");
  32.  
  33.     } catch (Exception ex) {
  34.         System.err.println(ex.getMessage());
  35.     }
  36. }
  37.  
  38. Map<Character, Long> rowReduce(String row) {
  39.     return Arrays.stream(row.split(","))
  40.             .flatMap(uuid -> uuid.chars().mapToObj(i -> (char) i))
  41.             .filter(ch -> ch != '-')
  42.             .collect(Collectors.toMap(Function.identity(), _ -> 1L, Long::sum));
  43. }
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement