fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Objects;
  10. import java.util.function.Function;
  11. import java.util.stream.Collectors;
  12. import java.util.stream.Stream;
  13.  
  14. /* Name of the class has to be "Main" only if the class is public. */
  15. class Ideone
  16. {
  17. public static void main (String[] args) throws java.lang.Exception
  18. {
  19. List<Student> list = new ArrayList<>(List.of(
  20. new Student(1, new ArrayList<>(List.of(new Marks(1, 10), new Marks(2, 10)))),
  21. new Student(1, new ArrayList<>(List.of(new Marks(1, 15), new Marks(3, 10)))),
  22. new Student(2, new ArrayList<>(List.of(new Marks(1, 5), new Marks(2, 10)))),
  23. new Student(2, new ArrayList<>(List.of(new Marks(2, 5), new Marks(3, 10))))
  24. ));
  25.  
  26. //Temporary map
  27. Map<Integer, Student> mapTemp = list.stream()
  28. .collect(Collectors.toMap(Student::getStudentId, //grouping by student id
  29. Function.identity(), //setting as value the student
  30. (s1, s2) -> new Student(s1.getStudentId(), new ArrayList<>( //Creating a new Student whose list of marks is given by the merged marks of the two colliding students
  31. Stream.concat(s1.getMarkList().stream(), s2.getMarkList().stream()) //Chaining the two lists of marks into a single stream
  32. .collect(Collectors.toMap(Marks::getSubjectId, //Grouping by the marks by the subject id
  33. Function.identity(), //Setting as value the mark
  34. (m1, m2) -> new Marks(m1.getSubjectId(), m1.getMark() + m2.getMark()))) //Handling the colliding marks by summing them together
  35. .values()))) //Retrieving the collection of merged marks
  36. );
  37.  
  38. //Result list with the merged students
  39. List listRes = new ArrayList(mapTemp.values());
  40. System.out.println(listRes);
  41. }
  42. }
  43.  
  44. class Student {
  45. private int studentId;
  46. private List<Marks> markList = new ArrayList<>();
  47.  
  48. public Student(int studentId, List<Marks> markList) {
  49. this.studentId = studentId;
  50. this.markList = markList;
  51. }
  52.  
  53. public int getStudentId() {
  54. return studentId;
  55. }
  56.  
  57. public List<Marks> getMarkList() {
  58. return markList;
  59. }
  60.  
  61. @Override
  62. public boolean equals(Object o) {
  63. if (this == o) return true;
  64. if (o == null || getClass() != o.getClass()) return false;
  65. Student student = (Student) o;
  66. return studentId == student.studentId;
  67. }
  68.  
  69. @Override
  70. public int hashCode() {
  71. return Objects.hash(studentId);
  72. }
  73.  
  74. @Override
  75. public String toString() {
  76. return String.format("[%d - %s]", studentId, markList);
  77. }
  78. }
  79.  
  80. class Marks {
  81. private Integer subjectId;
  82. private Integer mark;
  83.  
  84. public Marks(Integer subjectId, Integer mark) {
  85. this.subjectId = subjectId;
  86. this.mark = mark;
  87. }
  88.  
  89. public Integer getSubjectId() {
  90. return subjectId;
  91. }
  92.  
  93. public Integer getMark() {
  94. return mark;
  95. }
  96.  
  97. public void setSubjectId(Integer subjectId) {
  98. this.subjectId = subjectId;
  99. }
  100.  
  101. public void setMark(Integer mark) {
  102. this.mark = mark;
  103. }
  104.  
  105. @Override
  106. public boolean equals(Object o) {
  107. if (this == o) return true;
  108. if (o == null || getClass() != o.getClass()) return false;
  109. Marks marks = (Marks) o;
  110. return Objects.equals(subjectId, marks.subjectId);
  111. }
  112.  
  113. @Override
  114. public int hashCode() {
  115. return Objects.hash(subjectId);
  116. }
  117.  
  118. @Override
  119. public String toString() {
  120. return String.format("[%d - %d]", subjectId, mark);
  121. }
  122. }
  123.  
Success #stdin #stdout 0.12s 51244KB
stdin
Standard input is empty
stdout
[[1 - [[1 - 25], [2 - 10], [3 - 10]]], [2 - [[1 - 5], [2 - 15], [3 - 10]]]]