10 ways to Join two Lists in Java

In this post, we will see how to join two lists in Java using Plain Java, Java 8, Guava and Apache Commons Collections.

 

1. Plain Java

 

1. List.addAll()

List interface provides addAll(Collection) method that appends all elements of the specified collection to the end of the list. We can use it as:

We can also initialize the result list by first list using ArrayList constructor, thereby preventing an extra call to addAll().

 

2. Double Brace Initialization

We can also use Double Brace Initialization which internally creates an anonymous inner class with an instance initializer in it.

This technique should be best avoided as it costs an extra class at each usage and it also holds hidden references to the enclosing instance and to any captured objects. This may cause memory leaks or problems with serialization.

 

3. Collections.addAll()

Collections class provides several useful static utility methods that operate on collections. One such method is addAll(Collection, T[]) that adds all of the specified elements to the specified collection.

This method is similar to List.addAll() but likely to run significantly faster.

 

2. Java 8

We can also use Java 8 Stream API to join two lists.

 

1. Stream.of() + flatMap() + Collector

Here we’re obtaining stream consisting of elements from both the list using static factory method Stream.of() and accumulating all elements into a new list using a Collector.

 

2. Stream.of() + Stream.forEach()

We can avoid using a Collector by accumulating all elements using forEach() instead as shown below.

 

3. Stream.concat() + Collector

Stream provides concat() that takes two streams as input and creates a lazily concatenated stream whose elements are all the elements of the first stream followed by all the elements of the second stream.

 

4. List.addAll()

This is slight variation of List.addAll() approach discussed earlier by using streams in Java 8:

 

3. Guava

Guava Iterables class provides many static utility methods that operate on or return objects of type Iterable.

 

1. Iterables.concat()

concat() can be used to combine two iterables into a single iterable.

 

2. Iterables.addAll()

addAll() adds all elements in iterable to collection. We can use this in similar way as Collections's addAll() method.

 

4. Apache Commons Collections

Apache Commons Collections ListUtils class provides union() method that takes two lists as an input and returns a new list containing the second list appended to the first list.

 
 
Exercise: Concatenate Multiple Lists in Java

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
Sort by:   newest | oldest | most voted
Just_walking_here
Guest
Just_walking_here

I have one and only one question…. For the sake of God, why is it not possible to copy source code from this website? O_o What is the reason? ROTFL so hard beacuse of that xD

wpDiscuz