Advertisement
Guest User

Untitled

a guest
Oct 14th, 2017
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.44 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.util.Comparator;
  6. import java.util.Map;
  7. import java.util.TreeSet;
  8. import java.util.stream.Collectors;
  9.  
  10. public class Pr09StudentsByEnrollmentYear {
  11.  
  12.     public static void main(String[] args) {
  13.         final String dataFile = "src\\Lesson07BuiltInQueryMethodsStreamAPI\\Resources\\StudentData.txt";
  14.  
  15.         try (BufferedReader br = Files.newBufferedReader(Paths.get(dataFile))) {
  16.             br.lines()
  17.                     .skip(1)
  18.                     .map(line -> line.split("\\s+"))
  19.                     .collect(Collectors
  20.                             .groupingBy(
  21.                                     x -> "20" + x[0].substring(4) + ":",
  22.                                     Collectors.mapping(x -> "-- " + x[1] + " " + x[2],
  23.                                             Collectors.toCollection(TreeSet::new)
  24.                                     )
  25.                             )
  26.                     )
  27.                     .entrySet()
  28.                     .stream()
  29.                     .sorted(Comparator.comparing(Map.Entry::getKey))
  30.                     .forEachOrdered(entry -> {
  31.                         System.out.println(entry.getKey());
  32.                         entry.getValue().forEach(System.out::println);
  33.                     });
  34.         } catch (IOException e) {
  35.             e.printStackTrace();
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement