# Problem ordering nested collections

• June 11th, 2013, 07:24 AM
PauloSotto
Problem ordering nested collections
Hi all,

I know how to use Java collections but at beginner's level.

I have the list:

[10, 20, 30, 12, 13, 8, 1, 2, 3]

With the following methods:

1) List<List<Integer>> getSubSequences( Iterator<Integer> it, Comparator<Integer> cmp )
2) List<List<Integer>> getSubSequencesAscendentOrder( Iterator<Integer> it )

The outputs must be:

1) [[10, 20, 30], [12, 13], [8], [1, 2, 3]]

and

2) [[1, 2, 3], [8], [10, 20, 30], [12, 13]]

I've implemented the first method and allready works as follows:

Code :

```  class MyComparator implements Comparator<Integer> { public int compare(Integer i1, Integer i2) { return i1 - i2; } }     public static List<List<Integer>> getSubSequences(Iterator<Integer> it, Comparator<Integer> cmp) {   List<List<Integer>> list = new ArrayList<List<Integer>>(); ArrayList<Integer> subList = new ArrayList<Integer>();   int aux1 = 0;   if(it.hasNext()) { aux1 = it.next(); subList.add(aux1); }   while(it.hasNext()) { int aux2 = it.next();   if(cmp.compare(aux2, aux1) < 0 ) { list.add(subList); subList = new ArrayList<Integer>(); } subList.add(aux2); aux1 = aux2; } list.add(subLista); return list;   }```

In public static main() I have:

Code :

```    int array[] = {10, 20, 30, 12, 13, 8, 1, 2, 3};   ArrayList<Integer> li = new ArrayList<Integer>(array.length);   for(int i = 0; i < array.length; i++) li.add(array[i]);   Iterator<Integer> it = li.iterator();   Comparator cmp = new MyComparator(); List<List<Integer>> res = getSubSequences(it, cmp) ;   it = ((List)res).iterator();   while(it.hasNext()) System.out.print(it.next() + " "); System.out.println();```

This works fine and the output is [10, 20, 30] [12, 13] [8] [1, 2, 3]

Not exactly [[10, 20, 30], [12, 13], [8], [1, 2, 3]] but close.

I think the method 2) must be very similar to method 1), however I can't implement that.

Can someone give some clues?

Paulo
• June 11th, 2013, 11:42 AM
jps
Re: Problem ordering nested collections
Welcome PauloSotto

If the desired outputs are:
1) [[10, 20, 30], [12, 13], [8], [1, 2, 3]]
and:
2) [[1, 2, 3], [8], [10, 20, 30], [12, 13]]
...output 2 looks like output 1 sorted by the first element in ascending order.
• June 11th, 2013, 12:04 PM
PauloSotto
Re: Problem ordering nested collections
Quote:

Originally Posted by jps
Welcome PauloSotto

If the desired outputs are:
1) [[10, 20, 30], [12, 13], [8], [1, 2, 3]]
and:
2) [[1, 2, 3], [8], [10, 20, 30], [12, 13]]
...output 2 looks like output 1 sorted by the first element in ascending order.

You are absolutely right.

The problem is I don't know how to sort a List<List<Integer>> or say in other terms
I don't know how to sort the "inner" list inside the "outer" list.

Can you help me please ?

Thank you
Paulo
• June 11th, 2013, 12:46 PM
jps
Re: Problem ordering nested collections
The method signature of method2 shows an Iterator<Integer> as the parameter
Where will you get an Iterator<Integer>?
What happens if you sort the array before the first method runs? What is the output of the first method?
• June 11th, 2013, 06:08 PM
PauloSotto
Re: Problem ordering nested collections
Quote:

Originally Posted by jps
The method signature of method2 shows an Iterator<Integer> as the parameter
Where will you get an Iterator<Integer>?
What happens if you sort the array before the first method runs? What is the output of the first method?

Thank you very much for your patience.

I think is more informative paste the code than say what it does.

Lets see the entire code (it's not to big):

MyComparator.java
Code :

```public class MyComparator implements Comparator<Integer> { public int compare(Integer i1, Integer i2) { return i1 - i2; } }```

Main.java
Code :

```public class Main {   public static List<List<Integer>> getSubSequences(Iterator<Integer> it, Comparator<Integer> cmp) {   List<List<Integer>> lista = new ArrayList<List<Integer>>(); ArrayList <Integer> subLista = new ArrayList<Integer>();   int aux1 = 0;   if(it.hasNext()) // if has another element { aux1 = it.next(); // return element subLista.add(aux1); }   while(it.hasNext()) { int aux2 = it.next();   if(cmp.compare(aux2, aux1) < 0 ) { lista.add(subLista); subLista = new ArrayList<Integer>(); } subLista.add(aux2); aux1 = aux2; } lista.add(subLista);   return lista;   }       public static List<List<Integer>> getSubSequencesAscendentOrder(Iterator<Integer> it) { // THIS IS THE METHOD TO IMPLEMENT }       // M A I N   public static void main(String args[]) {   int array[] = {10, 20, 30, 12, 13, 8, 1, 2, 3};   // Create ArrayList with the same length of the array ArrayList<Integer> li = new ArrayList<Integer>(array.length);   // Copy array to ArrayList for(int i = 0; i < array.length; i++) { li.add(array[i]); }   Iterator<Integer> it = li.iterator();   Comparator cmp = new MyComparator(); List<List<Integer>> res = getSubSequences(it, cmp) ;   it = ((List)res).iterator();   while(it.hasNext()) System.out.print(it.next() + " "); System.out.println();   } }```

Method 1 => List<List<Integer>> getSubSequences( Iterator<Integer> it, Comparator<Integer> cmp )

Output:

[10, 20, 30] [12, 13] [8] [1, 2, 3]

This output however, is not accurate.

What was expected is:

[[10, 20, 30], [12, 13], [8], [1, 2, 3]]

(The leading '[' and trailing ']' and the commas between sub-lists are missing).

Method 2 => List<List<Integer>> getSubSequencesAscendentOrder( Iterator<Integer> it )

Expected output:

[[1, 2, 3], [8], [10, 20, 30], [12, 13]]

If you can help me I'll appreciate so much.

Paulo
• June 11th, 2013, 07:04 PM
jps
Re: Problem ordering nested collections
Code :

```public static List<List<Integer>> getSubSequencesAscendentOrder(Iterator<Integer> it) { // THIS IS THE METHOD TO IMPLEMENT }```
What have you tried here so far?
What happened when you tried my suggestion in post #4?
• June 12th, 2013, 04:17 AM
PauloSotto
Re: Problem ordering nested collections
Quote:

Originally Posted by jps
What have you tried here so far?
What happened when you tried my suggestion in post #4?

In post #4 you suggested:

Quote:

What happens if you sort the array before the first method runs? What is the output of the first method?
According to your suggestion, I get:

[1, 2, 3, 8, 10, 12, 13, 20, 30]

Since the array is sorted before calling first method, I'll get only one sub-collection because
the array can't be split in sub-arrays.

In second method I haven't tried nothing, because I don't know why can I sort the inner Lists inside the outer list.

Thank you

Paulo
• June 12th, 2013, 04:55 PM
PauloSotto
Ordering nested collections (question reformulated)
Hi all,

I don't know how to sort data structures of type List<List< ...>> or any collection of collections in general

I'll put here my question, but now, reformulated with a more concise (and simple) example.

Consider this code:

Code :

``` public static void main(String args[]) { ArrayList<String> list1 = new ArrayList<String>(); ArrayList<String> list2 = new ArrayList<String>(); ArrayList<String> list3 = new ArrayList<String>(); ArrayList<ArrayList<String>> listOfList = new ArrayList<ArrayList<String>>();   list1.add("9"); list1.add("6"); list1.add("3"); listOfList.add(list1);   list2.add("8"); list2.add("5"); list2.add("2"); listOfList.add(list2);   list3.add("7"); list3.add("4"); list3.add("1"); listOfList.add(list3);   Iterator<ArrayList<String>> it = listOfList.iterator(); while (it.hasNext()) { System.out.println(it.next()); }   }```

Output:

[9, 6, 3]
[8, 5, 2]
[7, 4, 1]

Now, I add the following code to the end of my program:

Code :

``` Collections.sort(listOfList.get(0)); // sort the 1st sublist => 0 -> 1st sublist; 1 -> 2nd sublist; 2 -> 3rd sublist   it = listOfList.iterator(); while (it.hasNext()) { System.out.println(it.next()); }```

Output:

[9, 6, 3]
[8, 5, 2]
[7, 4, 1]

[3, 6, 9] <= Now I have the 1st sublist by ascending order
[8, 5, 2]
[7, 4, 1]

But this is NOT what I want.

I want to sort not inside one sublist, but the sublists each other like this:

Before sorting:
[9, 6, 3] <= 1st sublist
[8, 5, 2] <= 2nd sublist
[7, 4, 1] <= 3rd sublist

After sorting:
[7, 4, 1] <= 1st sublist
[8, 5, 2] <= 2nd sublist
[9, 6, 3] <= 3rd sublist

I want to sort the sublists by the first element of each list.

Can anyone help me?

Paulo Sotto
• June 13th, 2013, 09:16 AM
KevinWorkman
Re: Ordering nested collections (question reformulated)
Sounds like a job for a custom Comparator.
• June 13th, 2013, 03:11 PM
jps
Re: Problem ordering nested collections
```ublic static List<List<Integer>> getSubSequencesAscendentOrder(Iterator<Integer> it) { // THIS IS THE METHOD TO IMPLEMENT }```