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.util.ArrayList;
  7. import java.util.List;
  8. import java.util.Objects;
  9. import java.util.stream.Collectors;
  10. import java.util.stream.IntStream;
  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<Pair> pairs = new ArrayList(List.of(
  18. new Pair(new ArrayList<>(List.of(new Endpoint("p1", 1000), new Endpoint("p2", 2000)))),
  19. new Pair(new ArrayList<>(List.of(new Endpoint("p1", 3000), new Endpoint("p3", 4000)))),
  20. new Pair(new ArrayList<>(List.of(new Endpoint("p2", 5000), new Endpoint("p3", 6000)))),
  21. new Pair(new ArrayList<>(List.of(new Endpoint("p1", 2000), new Endpoint("p2", 3000)))),
  22. new Pair(new ArrayList<>(List.of(new Endpoint("p1", 2001), new Endpoint("p2", 3001)))),
  23. new Pair(new ArrayList<>(List.of(new Endpoint("p1", 4000), new Endpoint("p3", 5000)))),
  24. new Pair(new ArrayList<>(List.of(new Endpoint("p1", 4001), new Endpoint("p3", 5001)))),
  25. new Pair(new ArrayList<>(List.of(new Endpoint("p2", 6000), new Endpoint("p3", 7000)))),
  26. new Pair(new ArrayList<>(List.of(new Endpoint("p2", 6001), new Endpoint("p3", 7001))))
  27. ));
  28.  
  29. System.out.println("Input: ");
  30. for (Pair p : pairs) {
  31. System.out.println(p);
  32. }
  33.  
  34. List<CustomPair> listRes = pairs.stream()
  35. .collect(Collectors.groupingBy(Pair::getPairKeys)) //Grouping the Pairs by the keys of the two EndPoints (key1:key2)
  36. .entrySet().stream() //Streaming the entries of the map
  37. .map(entry -> {
  38. String key1 = null, key2 = null;
  39.  
  40. //Lists for the values of the CustomEndpoint
  41. List<Integer> listValues1 = new ArrayList<>();
  42. List<Integer> listValues2 = new ArrayList<>();
  43.  
  44. //Retrieving the keys and adding each pair's number to the respective lists
  45. for (Pair p : entry.getValue()) {
  46. key1 = p.getPair().get(0).getKey();
  47. key2 = p.getPair().get(1).getKey();
  48.  
  49. listValues1.add(p.getPair().get(0).getNumber());
  50. listValues2.add(p.getPair().get(1).getNumber());
  51. }
  52.  
  53. //Returning the new CustomPair in place of the two grouped by Pair
  54. return new CustomPair(new ArrayList<>(List.of(
  55. new CustomEndpoint(key1, listValues1),
  56. new CustomEndpoint(key2, listValues2))));
  57. })
  58. .sorted(Comparator.comparing(CustomPair::getFirstPairInitialNumber)) //Ordering the CustomPair by the beginning of their endpoint range
  59. .collect(Collectors.toList()); //Collecting the CustomPair
  60.  
  61. System.out.println("\nOutput: ");
  62. for (CustomPair cp : listRes) {
  63. System.out.println(cp);
  64. }
  65. }
  66.  
  67. static class Endpoint {
  68. private String key;
  69. private Integer number;
  70.  
  71. public Endpoint(String key, Integer number) {
  72. this.key = key;
  73. this.number = number;
  74. }
  75.  
  76. public String getKey() {
  77. return key;
  78. }
  79.  
  80. public Integer getNumber() {
  81. return number;
  82. }
  83.  
  84. @Override
  85. public boolean equals(Object o) {
  86. if (this == o) return true;
  87. if (o == null || getClass() != o.getClass()) return false;
  88. Endpoint other = (Endpoint) o;
  89. return Objects.equals(key, other.key) && Objects.equals(number, other.number);
  90. }
  91.  
  92. @Override
  93. public int hashCode() {
  94. return Objects.hash(key, number);
  95. }
  96.  
  97. @Override
  98. public String toString() {
  99. return String.format("{%s,%d}", key, number);
  100. }
  101. }
  102.  
  103. static class Pair {
  104. // It will have exactly 2 entries
  105. List<Endpoint> pair;
  106.  
  107. public Pair(List<Endpoint> pair) {
  108. this.pair = pair;
  109. }
  110.  
  111. public List<Endpoint> getPair() {
  112. return pair;
  113. }
  114.  
  115. public String getPairKeys() {
  116. if (pair == null || pair.size() < 2) {
  117. return null;
  118. }
  119.  
  120. Endpoint e1 = pair.get(0);
  121. Endpoint e2 = pair.get(1);
  122.  
  123. return String.format("%s:%s", e1.getKey(), e2.getKey());
  124. }
  125.  
  126. @Override
  127. public boolean equals(Object o) {
  128. if (this == o) return true;
  129. if (o == null || getClass() != o.getClass()) return false;
  130. Pair other = (Pair) o;
  131. return Objects.equals(pair, other.pair);
  132. }
  133.  
  134. @Override
  135. public int hashCode() {
  136. return Objects.hash(pair);
  137. }
  138.  
  139. @Override
  140. public String toString() {
  141. return String.format("%s", pair);
  142. }
  143. }
  144.  
  145. static class CustomEndpoint {
  146. private String key;
  147. private List<Integer> numbers;
  148.  
  149. public CustomEndpoint(String key, List<Integer> numbers) {
  150. this.key = key;
  151. this.numbers = numbers;
  152. }
  153.  
  154. public List<Integer> getNumbers() {
  155. return numbers;
  156. }
  157.  
  158. @Override
  159. public String toString() {
  160. return String.format("{%s,%s}", key, numbers);
  161. }
  162. }
  163.  
  164. static class CustomPair {
  165. // It will have exactly 2 entries
  166. List<CustomEndpoint> pair;
  167.  
  168. public CustomPair(List<CustomEndpoint> pair) {
  169. this.pair = pair;
  170. }
  171.  
  172. public Integer getFirstPairInitialNumber() {
  173. return pair.get(0).getNumbers().get(0);
  174. }
  175.  
  176. @Override
  177. public String toString() {
  178. return pair.toString();
  179. }
  180. }
  181. }
Success #stdin #stdout 0.12s 51252KB
stdin
Standard input is empty
stdout
Input: 
[{p1,1000}, {p2,2000}]
[{p1,3000}, {p3,4000}]
[{p2,5000}, {p3,6000}]
[{p1,2000}, {p2,3000}]
[{p1,2001}, {p2,3001}]
[{p1,4000}, {p3,5000}]
[{p1,4001}, {p3,5001}]
[{p2,6000}, {p3,7000}]
[{p2,6001}, {p3,7001}]

Output: 
[{p1,[1000, 2000, 2001]}, {p2,[2000, 3000, 3001]}]
[{p1,[3000, 4000, 4001]}, {p3,[4000, 5000, 5001]}]
[{p2,[5000, 6000, 6001]}, {p3,[6000, 7000, 7001]}]