How to append a list to another list in java

append, java, list, swing

Solution

When programming in Java, you should be referencing the Javadoc for the class or interface you're using to answer these sorts of questions.

You would use:

books2.addAll(books);

to append all entries in `books` to `books2`.

From the Javadoc linked above:

boolean addAll(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Problem

here is my 2 lists and i want to append the values of list books to book2 for correcting the output ``` List<Book>books=null; List<Book> books2=null; String searchParam=null; searchParam=txtSearch.getText(); if(category.isSelected()){ List<Category> categorys =null; categorys=ServiceFactory.getCategoryServiceImpl().findAllByName(searchParam); for(Category i:categorys){ books=ServiceFactory.getBookServiceImpl().findAllBookByCategoryId(i.getId()); System.out.println(i.getName()+books+"hi.."); ``` here append or add list to another ``` if(books!=null){ books2=books.add(books2); } } } for(Book book:books){ txtSearchResult.append(book.getName()+"\n"); } ``` error shows ``` The method add(Book) in the type List<Book> is not applicable for the arguments (List<Book>) ``` if u know the answer please share here..

Original source