/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main
(final String[] args
) { //Your input of [[1,0],[2,0],[3,3],[1,0,0]] in a List of Lists
final List<List<Integer>> input = createInput();
//To hold onto the groups for printing later
final Map
<Integer, List
<List
<Integer
>>> groupsMap
= new HashMap
<Integer, List
<List
<Integer
>>>();
//This will track the List<Integer> that have already been grouped.
final List<List<Integer>> ungroupedLists = new ArrayList<List<Integer>>(input);
for (int i = 0; i < input.size(); i++ ) {
//This gets the group indexed at i (starting from 0 just like arrays)
final List<Integer> currentList = input.get(i);
//This checks if the currentList has already been grouped.
//If it has, then continue to the next List.
if( !ungroupedLists.contains(currentList) ) {
continue;
}
//If it hasn't already been grouped, then remove it as we're going to group it now.
ungroupedLists.remove(currentList);
//Create a new group to add any grouped lists to.
final List<List<Integer>> currentGroup = new ArrayList<List<Integer>>();
currentGroup.add(currentList);
//For the ungrouped lists, check if they group with the current list.
for (int j = 0; j < ungroupedLists.size(); j++) {
//Get the list we want to compare the current one to.
final List<Integer> currentComparisonList = ungroupedLists.get(j);
//Gets the first integer of the currentList
final Integer integer
= currentList.
get(0); //Gets the last integer of the currentList
final Integer integer2
= currentList.
get(currentList.
size()-1); //Gets the first integer of the currentComparisonList
final Integer integer3
= currentComparisonList.
get(0); //Gets the last integer of the currentComparisonList
final Integer integer4
= currentComparisonList.
get(currentComparisonList.
size()-1);
//If the first Integers match, and the last integers match, add the currentComparisonList
//To the group and remove it from the ungroupedLists collection
if( integer == integer3 && integer2 == integer4 ) {
currentGroup.add(currentComparisonList);
ungroupedLists.remove(currentComparisonList);
}
}
//When we've done all of that checking, add the group to the map of groups.
groupsMap.put(i, currentGroup);
}
//This simply prints the output int he format desired.
for ( int i = 0; i < groupsMap.size(); i++ ) {
System.
out.
println("group: " + i
); for (final List<Integer> list : groupsMap.get(i)) {
System.
out.
println(list.
toString()); }
}
}
private static List<List<Integer>> createInput() {
final List<List<Integer>> input = new ArrayList<List<Integer>>();
final ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(1);
list1.add(0);
final ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(2);
list2.add(0);
final ArrayList<Integer> list3 = new ArrayList<Integer>();
list3.add(3);
list3.add(3);
final ArrayList<Integer> list4 = new ArrayList<Integer>();
list4.add(1);
list4.add(0);
list4.add(0);
input.add(list1);
input.add(list2);
input.add(list3);
input.add(list4);
return input;
}
}