Results 1 to 17 of 17
  1. #1
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Combining 3 list into one

    I'm trying to combine three list which I pass into a single list


    getPeopleReadingBooks(15, fictionBooks, alienBooks, scienceBooks);

    public Map<String, List<BookUser>> getPeopleReadingBooks(int bookReaders, List<Book>... bookLists){


    }

    I know of the following approach but this doesnt work :
    List<Book> newList = new ArrayList<Book>() { { addAll(fictionBooks); addAll(alienBooks); addAll(scienceBooks) } };

    Any suggestions..

    Thank you !

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Combining 3 list into one

    Rethink the way you are using the addAll method. That should be the method to use.
    Do you want one list with all the elements from the three lists or do you want a list that contains the three lists?

  3. #3
    kevinn205 is offline Member
    Join Date
    Nov 2011
    Posts
    65
    Rep Power
    0

    Default Re: Combining 3 list into one

    one possiblity is to read each list with a loop then add them to a new list that contains all three

    not the most efficent but it gets the job done

  4. #4
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Re: Combining 3 list into one

    Quote Originally Posted by Norm View Post
    Rethink the way you are using the addAll method. That should be the method to use.
    Do you want one list with all the elements from the three lists or do you want a list that contains the three lists?
    The number of list's is variable, I require a list that contains the three lists (ConsolidatedList) which I can then pass as a paremeter to another method.

    Thanks!

  5. #5
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Combining 3 list into one

    Quote Originally Posted by linking90 View Post
    I know of the following approach but this doesnt work :
    List<Book> newList = new ArrayList<Book>() { { addAll(fictionBooks); addAll(alienBooks); addAll(scienceBooks) } };

    Any suggestions..

    Thank you !
    Can you post the error you get for this?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Combining 3 list into one

    I require a list that contains the three lists
    Are you asking for a list of lists? One list that has three elements.

  7. #7
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Re: Combining 3 list into one

    Quote Originally Posted by Norm View Post
    Are you asking for a list of lists? One list that has three elements.
    Yes that's exactly what I meant !

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Combining 3 list into one

    Define an ArrayList that is to hold ArrayList<Book> and add the three ArrayList<Book> to it.
    ArrayList<ArrayList<Book>>

  9. #9
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Re: Combining 3 list into one

    Quote Originally Posted by Norm View Post
    Define an ArrayList that is to hold ArrayList<Book> and add the three ArrayList<Book> to it.
    ArrayList<ArrayList<Book>>
    Thanks but the problem I'm facing is in accessing the arguments of the add method.

    List<List<Book>> bookList = new ArrayList<List<Book>>();
    bookList.add(fictionBooks);
    bookList.add(alienBooks);
    bookList.add(scienceBooks);

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Combining 3 list into one

    but the problem I'm facing is in accessing the arguments of the add method.
    What problem? Please explain.
    Please post the full text of your errors.

  11. #11
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Re: Combining 3 list into one

    Method I use to make the call
    public void getPeopleReadingBooks(15, fictionBooks, alienBooks, scienceBooks);

    I cant compile the program as there is a red line on the arguments of the add method.


    public void getPeopleReadingBooks(int bookReaders, List<Book>... bookLists){

    List<List<Book>> bookList = new ArrayList<List<Book>>();
    bookList.add(fictionBooks);
    bookList.add(alienBooks);
    bookList.add(scienceBooks);
    }

    Thanks Norm for your quick response !

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Combining 3 list into one

    there is a red line
    Sorry. A red line has no text.
    You need to compile the code and get the compiler's error messages.

  13. #13
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Re: Combining 3 list into one

    Quote Originally Posted by Norm View Post
    Sorry. A red line has no text.
    You need to compile the code and get the compiler's error messages.
    fictionBooks cannot be resolved to a variable.
    alienBooks cannot be resolved to a variable.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Combining 3 list into one

    Are the definitions for those variable in scope where you are trying to use them?

  15. #15
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Re: Combining 3 list into one

    The values get passed but its not in the scope so I'm not sure how to access them. This is a huge application, build time is like 6 minutes.


    public void getPeopleReadingBooks(int bookReaders, List<Book>... bookLists){

    Can I write something like

    bookList.add(List <Book> bookLists);

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Combining 3 list into one

    Can I write something like
    Have you read the API doc for the add() method? What is the data type of your args to the add method?

    When in doubt ask the compiler by trying to compile it.

  17. #17
    linking90 is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default Re: Combining 3 list into one

    I think this is it ! Thanks Norm and everyone for all your posts !

    public void getPeopleReadingBooks(int bookReaders, List<Book>... bookLists){


    List<List<Book>> consbook = new ArrayList<List<Book>>();

    for ( List<Book> b : bookLists ){
    consbook.add(b);
    }

Similar Threads

  1. Combining shapes?
    By snj00u in forum Java 2D
    Replies: 2
    Last Post: 06-14-2011, 09:09 PM
  2. Java combining arrays
    By trakke in forum New To Java
    Replies: 5
    Last Post: 04-28-2011, 09:32 PM
  3. Combining these 2 projects
    By fresh83 in forum New To Java
    Replies: 14
    Last Post: 12-28-2009, 08:52 AM
  4. combining two separate codes into one
    By jaiminparikh in forum Advanced Java
    Replies: 22
    Last Post: 02-18-2009, 05:35 PM
  5. Help combining loops into 1 program.
    By kewlgeye in forum New To Java
    Replies: 5
    Last Post: 04-22-2008, 09:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •