Advertisement
Guest User

Untitled

a guest
Jul 9th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. package GroupByGroup;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.util.*;
  8. import java.util.stream.Collectors;
  9. import java.util.stream.Stream;
  10.  
  11. public class Main {
  12.     public static void main(String[] args) throws IOException {
  13.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  14.         Set<Person> persons = new HashSet<Person>();
  15.         String line = reader.readLine();
  16.  
  17.         while(!line.equals("END")){
  18.             String[] info = line.split(" ");
  19.             persons.add(new Person(info[0]+" "+info[1],Integer.parseInt(info[2])));
  20.             line = reader.readLine();
  21.         }
  22.  
  23.         Map<Integer, Set<String>> result =
  24.                 persons.stream()
  25.                         .collect(
  26.                         Collectors.groupingBy(Person::getGroup,
  27.                                 Collectors.mapping(Person::getName,Collectors.toSet()))
  28.                 );
  29.  
  30.         result
  31.                 .entrySet()
  32.                 .stream()
  33.                // .sorted(Comparator.comparing(a -> a.getKey()))
  34.                 .forEach(a -> System.out.println(a.getKey()+" - "+String.join(", ",a.getValue())));
  35.  
  36.     }
  37.  
  38.     public static class Person {
  39.         private Integer group;
  40.         private String name;
  41.  
  42.         public Integer getGroup() {
  43.             return group;
  44.         }
  45.  
  46.         public void setGroup(Integer group) {
  47.             this.group = group;
  48.         }
  49.  
  50.         public String getName() {
  51.             return name;
  52.         }
  53.  
  54.         public void setName(String name) {
  55.             this.name = name;
  56.         }
  57.  
  58.         public Person(String name, Integer group) {
  59.             this.group = group;
  60.             this.name = name;
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement