Python join two lists with separator

This post will discuss how to join two Java lists using Plain Java, Java 8, Guava, and Apache Commons Collections.

1. Plain Java

⮚ Using List.addAll[]

List interface provides the addAll[Collection] method that appends all elements of the specified collection at the end of the list. We can use it as follows:

1
2
3
4
5
6
7
8
9
10
// Generic method to join two lists in Java
public static List merge[List list1, List list2]
{
List list = new ArrayList[];
list.addAll[list1];
list.addAll[list2];
return list;
}

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

1
2
3
4
5
6
7
8
// Generic method to join two lists in Java
public static List merge[List list1, List list2]
{
List list = new ArrayList[list1];
list.addAll[list2];
return list;
}

⮚ Double Brace Initialization

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

1
2
3
4
5
6
7
8
// Generic method to join two lists in Java
public static List merge[List list1, List list2]
{
return new ArrayList[] {{
addAll[list1];
addAll[list2];
}};
}


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

⮚ Using Collections.addAll[]

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

1
2
3
4
5
6
7
8
9
10
// Method to join two lists in Java
public static List merge[List list1, List list2]
{
List list = new ArrayList[];
Collections.addAll[list, list1.toArray[new String[0]]];
Collections.addAll[list, list2.toArray[new String[0]]];
return list;
}


This method is similar to List.addAll[] but likely to run significantly faster.

2. Using Java 8

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

⮚ Using Stream.of[] with flatMap[] + Collector

Here were obtaining a stream consisting of elements from both the list using the static factory method Stream.of[] and accumulating all elements into a new list using a Collector.

1
2
3
4
5
6
7
// Generic method to join two lists in Java
public static List merge[List list1, List list2]
{
return Stream.of[list1, list2]
.flatMap[x -> x.stream[]]
.collect[Collectors.toList[]];
}

⮚ Using Stream.of[] with Stream.forEach[]

We can avoid using a collector by accumulating all elements using forEach[] instead, as shown below:

1
2
3
4
5
6
7
8
// Generic method to join two lists in Java
public static List merge[List list1, List list2]
{
List list = new ArrayList[];
Stream.of[list1, list2].forEach[list::addAll];
return list;
}

⮚ Using Stream.concat[] + Collector

The Java 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.

1
2
3
4
5
6
// Generic method to join two lists in Java
public static List merge[List list1, List list2]
{
return Stream.concat[list1.stream[], list2.stream[]]
.collect[Collectors.toList[]];
}

⮚ Using List.addAll[]

This is a slight variation of the List.addAll[] approach discussed earlier by using streams in Java 8 and above:

1
2
3
4
5
6
7
8
// Generic method to join two lists in Java
public static List merge[List list1, List list2]
{
List list = list1.stream[].collect[Collectors.toList[]];
list.addAll[list2];
return list;
}

3. Using Guava Library

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

⮚ Using Iterables.concat[]

concat[] can be used to combine two iterables into a single iterable.

1
2
3
4
// Generic method to join two lists in Java
public static List merge[List list1, List list2] {
return Lists.newArrayList[Iterables.concat[list1, list2]];
}

⮚ Using Iterables.addAll[]

addAll[] adds all elements in iterable to collection. We can use this in a similar way as Collectionss addAll[] method.

1
2
3
4
5
6
7
8
9
10
// Generic method to join two lists in Java
public static List merge[List list1, List list2]
{
List list = Lists.newArrayList[];
Iterables.addAll[list, list1];
Iterables.addAll[list, list2];
return list;
}

4. Using Apache Commons Collections

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

1
2
3
4
// Generic method to join two lists in Java
public static List merge[List list1, List list2] {
return ListUtils.union[list1, list2];
}

Thats all about joining two lists in Java.


Exercise: Concatenate Multiple lists in Java

Video liên quan

Chủ Đề