fork 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.time.LocalDate;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.stream.Collectors;
  10. import java.util.stream.Stream;
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15. public static void main (String[] args) throws java.lang.Exception
  16. {
  17. List<Exon> listExon = new ArrayList<>(List.of(
  18. new Exon(1L, new IncomeCode("45"), LocalDate.of(2021, 1, 1), "4"),
  19. new Exon(2L, new IncomeCode("21"), LocalDate.of(2022, 1, 1), "5"),
  20. new Exon(3L, new IncomeCode("33"), LocalDate.of(2023, 1, 1), "2"),
  21. new Exon(4L, new IncomeCode("45"), LocalDate.of(2021, 1, 1), "4")
  22. ));
  23.  
  24. List<Sup> listSup = new ArrayList<>(List.of(
  25. new Sup(1L, new IncomeCode("45"), null, LocalDate.of(2021, 1, 1), "4"),
  26. new Sup(2L, new IncomeCode("21"), null, LocalDate.of(2022, 1, 1), "5"),
  27. new Sup(3L, new IncomeCode("33"), null, LocalDate.of(2023, 1, 1), "2")
  28. ));
  29.  
  30. List<List<Object>> listRes = new ArrayList<>(Stream.concat(listExon.stream(), listSup.stream())
  31. .collect(Collectors.groupingBy(obj -> {
  32. if (obj instanceof Exon) {
  33. Exon exon = (Exon) obj;
  34. return String.format("%s-%s-%s", exon.getIncomeCode(), exon.getEndDate(), exon.getCodeRef());
  35. }
  36. Sup sup = (Sup) obj;
  37. return String.format("%s-%s-%s", sup.getIncomeCode(), sup.getEndDate(), sup.getCodeRef());
  38. })).values());
  39.  
  40. for(List<Object> l: listRes){
  41. System.out.println(l);
  42. }
  43. }
  44.  
  45. static class Exon {
  46. private Long id;
  47. private IncomeCode incomeCode;
  48. private LocalDate endDate;
  49. String codeRef;
  50.  
  51. public Exon(Long id, IncomeCode incomeCode, LocalDate endDate, String codeRef) {
  52. this.id = id;
  53. this.incomeCode = incomeCode;
  54. this.endDate = endDate;
  55. this.codeRef = codeRef;
  56. }
  57.  
  58. public Long getId() {
  59. return id;
  60. }
  61.  
  62. public IncomeCode getIncomeCode() {
  63. return incomeCode;
  64. }
  65.  
  66. public LocalDate getEndDate() {
  67. return endDate;
  68. }
  69.  
  70. public String getCodeRef() {
  71. return codeRef;
  72. }
  73.  
  74. @Override
  75. public String toString() {
  76. return String.format("%s{%s %s %s}", getClass().getSimpleName(), incomeCode, endDate, codeRef);
  77. }
  78. }
  79.  
  80. static class Sup {
  81. private Long id;
  82. private IncomeCode incomeCode;
  83. private LocalDate startDate;
  84. private LocalDate endDate;
  85. String codeRef;
  86.  
  87. public Sup(Long id, IncomeCode incomeCode, LocalDate startDate, LocalDate endDate, String codeRef) {
  88. this.id = id;
  89. this.incomeCode = incomeCode;
  90. this.startDate = startDate;
  91. this.endDate = endDate;
  92. this.codeRef = codeRef;
  93. }
  94.  
  95. public Long getId() {
  96. return id;
  97. }
  98.  
  99. public IncomeCode getIncomeCode() {
  100. return incomeCode;
  101. }
  102.  
  103. public LocalDate getStartDate() {
  104. return startDate;
  105. }
  106.  
  107. public LocalDate getEndDate() {
  108. return endDate;
  109. }
  110.  
  111. public String getCodeRef() {
  112. return codeRef;
  113. }
  114.  
  115. @Override
  116. public String toString() {
  117. return String.format("%s{%s %s %s}", getClass().getSimpleName(), incomeCode, endDate, codeRef);
  118. }
  119. }
  120.  
  121. static class IncomeCode {
  122. private String code;
  123.  
  124. public IncomeCode(String code) {
  125. this.code = code;
  126. }
  127.  
  128. public String getCode() {
  129. return code;
  130. }
  131.  
  132. @Override
  133. public String toString() {
  134. return code;
  135. }
  136. }
  137. }
Success #stdin #stdout 0.12s 50012KB
stdin
Standard input is empty
stdout
[Exon{45 2021-01-01 4}, Exon{45 2021-01-01 4}, Sup{45 2021-01-01 4}]
[Exon{33 2023-01-01 2}, Sup{33 2023-01-01 2}]
[Exon{21 2022-01-01 5}, Sup{21 2022-01-01 5}]