user5032587
user5032587

Reputation:

Adding two List<String> in one List<String>

I have two Lists

List<String> list1 =new ArrayList<String>() ;
List<String> list2 =new ArrayList<String>() ;

I want these two lists to append one after other and send to a set method for my bean. Please let me know what is the best way to combine these two lists.

Upvotes: 0

Views: 93

Answers (4)

Jean Logeart
Jean Logeart

Reputation: 53819

Simply:

List<String> appended = new ArrayList<>(lis1); // copy of list1
appended.addAll(list2);                      // add all elements of list2

appended is a new List containing all elements from list1 and list2. No list got hurt during the process.

Upvotes: 2

Reece Kenney
Reece Kenney

Reputation: 2964

To combine the lists, you can just do addAll like so:

list1.addAll(list2) //Adds list1 to list2

Link to the addAll documentation

Upvotes: 1

LychmanIT
LychmanIT

Reputation: 725

You can use .addAll() to add the elements of the second list to the first:

array1.addAll(array2);

Upvotes: 1

ByeBye
ByeBye

Reputation: 6946

You can do

list1.addAll(list2)

That will add all elements from list2 at end of list1

Upvotes: 3

Related Questions