fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10.  
  11. public static void main(final String[] args) {
  12. //Your input of [[1,0],[2,0],[3,3],[1,0,0],[2,0],[2,0,0],[0,0],[1,2] ] in a List of Lists
  13. final List<List<String>> input = createInput();
  14.  
  15. //To hold onto the groups for printing later
  16. final Map<Integer, List<List<String>>> groupsMap = new HashMap<Integer, List<List<String>>>();
  17.  
  18. //This will track the List<Integer> that have already been grouped.
  19. final List<List<String>> ungroupedLists = new ArrayList<List<String>>(input);
  20.  
  21. //Have a separate groupCounter so that we end up with continuous group numbers
  22. int groupCounter = 0;
  23.  
  24. for (int i = 0; i < input.size(); i++ ) {
  25. //This gets the group indexed at i (starting from 0 just like array
  26. final List<String> currentList = input.get(i);
  27.  
  28. //This checks if the currentList has already been grouped.
  29. //If it has, then continue to the next List.
  30. if( !ungroupedLists.contains(currentList) ) {
  31. continue;
  32. }
  33. //If it hasn't already been grouped, then remove it as we're going to group it now.
  34. ungroupedLists.remove(currentList);
  35.  
  36. //Create a new group to add any grouped lists to.
  37. final List<List<String>> currentGroup = new ArrayList<List<String>>();
  38. currentGroup.add(currentList);
  39.  
  40. //This list will hold the lists that get grouped so they can be removed for subsequent grouping runs.
  41. final List<List<String>> groupedLists = new ArrayList<List<String>>();
  42.  
  43. //For the ungrouped lists, check if they group with the current list.
  44. for (int j = 0; j < ungroupedLists.size(); j++) {
  45. //Get the list we want to compare the current one to.
  46. final List<String> currentComparisonList = ungroupedLists.get(j);
  47.  
  48. //Gets the first integer of the currentList
  49. final String integer = currentList.get(0);
  50. //Gets the last integer of the currentList
  51. final String integer2 = currentList.get(currentList.size()-1);
  52. //Gets the first integer of the currentComparisonList
  53. final String integer3 = currentComparisonList.get(0);
  54. //Gets the last integer of the currentComparisonList
  55. final String integer4 = currentComparisonList.get(currentComparisonList.size()-1);
  56.  
  57. //If the first Integers match, and the last integers match, add the currentComparisonList
  58. //to the group and makr it for removal later
  59. if( integer.equals(integer3) && integer2.equals(integer4) ) {
  60. currentGroup.add(currentComparisonList);
  61. groupedLists.add(currentComparisonList);
  62. }
  63. }
  64.  
  65. //Remove the now grouped lists.
  66. ungroupedLists.removeAll(groupedLists);
  67.  
  68. //When we've done all of that checking, add the group to the map of groups.
  69. groupsMap.put(groupCounter, currentGroup);
  70.  
  71. //Finally increment the groupCounter for the next group
  72. groupCounter++;
  73. }
  74.  
  75. //This simply prints the output in the format desired.
  76. for ( int i = 0; i < groupsMap.size(); i++ ) {
  77. System.out.println("group: " + i);
  78. for (final List<String> list : groupsMap.get(i)) {
  79. System.out.println(list.toString());
  80. }
  81. }
  82. }
  83.  
  84. private static List<List<String>> createInput() {
  85. final List<List<String>> input = new ArrayList<List<String>>();
  86. final ArrayList<String> list1 = new ArrayList<String>();
  87. list1.add("1");
  88. list1.add("0");
  89.  
  90. final ArrayList<String> list2 = new ArrayList<String>();
  91. list2.add("2");
  92. list2.add("0");
  93.  
  94. final ArrayList<String> list3 = new ArrayList<String>();
  95. list3.add("3");
  96. list3.add("3");
  97.  
  98. final ArrayList<String> list4 = new ArrayList<String>();
  99. list4.add("1");
  100. list4.add("0");
  101. list4.add("0");
  102.  
  103. final ArrayList<String> list5 = new ArrayList<String>();
  104. list5.add("2");
  105. list5.add("0");
  106.  
  107. final ArrayList<String> list6 = new ArrayList<String>();
  108. list6.add("2");
  109. list6.add("0");
  110. list6.add("0");
  111.  
  112. final ArrayList<String> list7 = new ArrayList<String>();
  113. list7.add("0");
  114. list7.add("0");
  115.  
  116. final ArrayList<String> list8 = new ArrayList<String>();
  117. list8.add("1");
  118. list8.add("2");
  119.  
  120. input.add(list1);
  121. input.add(list2);
  122. input.add(list3);
  123. input.add(list4);
  124. input.add(list5);
  125. input.add(list6);
  126. input.add(list7);
  127. input.add(list8);
  128. return input;
  129. }
  130. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
group: 0
[1, 0]
[1, 0, 0]
group: 1
[2, 0]
[2, 0]
[2, 0, 0]
group: 2
[3, 3]
group: 3
[0, 0]
group: 4
[1, 2]