Join multiple lists in Java

How to Join multiple lists in Java

In this short post, we will learn how to join multiple lists in Java. We will be covering how to do this in Java 8, Java 7 and how we can use Apache Common API to join these lists.

 

1.  Using  Java 8 Stream API

Java 8 Stream API provides a couple of different options to join multiple lists. We will be exploring concat() and flatMap() method provided by the Stream API.

 

1. 1 Using  Concat method

We can use concat() method to combine two streams, concat() method will work by contacting all elements of the first stream with the second stream.

public class JoinListJava8 {
public static void main(String[] args) {
List<String> listA= asList("A", "B","C");
List<String> listB=asList("C","D","E","F");
concactStream(listA,listB);
}
public static void concactStream(List<String> listA, List<String> listB){
List<String> newList= Stream.concat(listA.stream(),listB.stream()).collect(Collectors.toList());
newList.forEach(item-> System.out.println(item));
}
}

Output


A
B
C
C
D
E
F

In case we have more than 2 Streams to combine, we can use concat() method within original method

Stream.concat(Stream.concat(c.stream(), c2.stream()), c3.stream());
 
1. 2 Using  flatMap method

In case we have a number of lists, we can use flatMap() method to join multiple lists in Java.flatMap method will return a Stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element.

Please read Stream#flatMap for more detail

 

public class JoinListJava8 {
public static void main(String[] args) {
List<String> listA= asList("A", "B","C");
List<String> listB=asList("C","D","E","F");
flatMap(listA,listB);
}
public static void flatMap(List<String> listA, List<String> listB){
List<String> newList= Stream.of(listA,listB).flatMap(Collection::stream).collect(Collectors.toList());
newList.forEach(item-> System.out.println(item));
}
}

 

2.  Using  Java 7 

If you are not using Java 8 and do not want to use any third party API (like Apache Common Collections or Guava), you can use addAll() method to accomplish your goal. You need to call addAll() method multiple times to join lists in Java.

public class JoinListJava7 {
public static void main(String[] args) {
List<String> listA= asList("A", "B","C");
List<String> listB=asList("C","D","E","F");
List<String> listC = new ArrayList<String>();
listC.addAll(listA);
listC.addAll(listB);
//print results
for(String output: listC){
System.out.println(output);
}
}
}

You can even shorten above code by using 

List<String> listC = new ArrayList<String>(listA);
listC.addAll(listB);

 

3. Apache Commons Collections

Apache Commons Collections API provides utility methods to join multiple lists in Java.

public class JoinListApacheCommons {
public static void main(String[] args) {
List<String> listA= asList("A", "B","C");
List<String> listB=asList("C","D","E","F");
Iterable<String> newList= CollectionUtils.union(listA,listB);
}
}

 

4. Summary

In this post, we discussed how to join multiple lists in Java. We covered how to do it in JDK using Java 7 and Java 8 along with an option to use Apache Common Collection API.

All the code of this article is available Over on Github. This is a Maven-based project.

You May Also Like