Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Problem ordering nested collections

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:


     
    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:


     
     
        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?


    Thank you in advance

    Paulo


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem ordering nested collections

    Quote Originally Posted by jps View Post
    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

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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?

  5. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem ordering nested collections

    Quote Originally Posted by jps View Post
    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
    public class MyComparator implements Comparator<Integer> 
    {
        public int compare(Integer i1, Integer i2)
        {
            return i1 - i2;
        }
    }


    Main.java
    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.


    Thank you in advance.

    Paulo

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problem ordering nested collections

    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?

  7. #7
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem ordering nested collections

    Quote Originally Posted by jps View Post
    What have you tried here so far?
    What happened when you tried my suggestion in post #4?
    In post #4 you suggested:

    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

  8. #8
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:

        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:

            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?


    Thank you in advance.

    Paulo Sotto

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Ordering nested collections (question reformulated)

    Sounds like a job for a custom Comparator.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Problem ordering nested collections

    Please do not double post your question.
    Threads merged.

    ublic static List<List<Integer>> getSubSequencesAscendentOrder(Iterator<Integer> it)
        {
            // THIS IS THE METHOD TO IMPLEMENT        
        }
    This does not show any effort to solve this problem. What have you tried?

Similar Threads

  1. Replies: 1
    Last Post: November 19th, 2012, 05:44 AM
  2. Having some nested loop problem
    By joel1314 in forum Loops & Control Statements
    Replies: 13
    Last Post: June 3rd, 2012, 05:04 AM
  3. Nested-If Problem/ Initializing
    By ak120691 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 2nd, 2011, 08:37 PM
  4. Deciding Interceptor Ordering?
    By arpitgadle in forum Web Frameworks
    Replies: 1
    Last Post: August 10th, 2010, 05:33 AM
  5. [SOLVED] Problem with Ordering an Arraylist of Comparable Objects.
    By Faz in forum Collections and Generics
    Replies: 8
    Last Post: June 16th, 2010, 06:36 PM

Tags for this Thread