How do I join two lists in Java?

java 8 combine two lists
merge two arraylist of objects in java
java merge two lists different types
java merge two lists without duplicates
java inner join two lists
java-stream two lists
how to add one list to another list in java 8
java 8 merge two objects

Conditions: do not modifiy the original lists; JDK only, no external libraries. Bonus points for a one-liner or a JDK 1.3 version.

Is there a simpler way than:

List<String> newList = new ArrayList<String>();
newList.addAll(listOne);
newList.addAll(listTwo);

Off the top of my head, I can shorten it by one line:

List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);

10 ways to Join two Lists in Java, How do you add a list to a list in Java? Example 1: Join Two Lists using addAll() When you run the program, the output will be: In the above program, we used List's addAll() method to join lists list1 and list2 to the joined list. Example 2: Join Two Lists using union() The output of this program is the same.

In Java 8:

List<String> newList = Stream.concat(listOne.stream(), listTwo.stream())
                             .collect(Collectors.toList());

How do I join two lists in Java?, 3. Merge two arraylists without duplicates. Use LinkedHashSet. A set allow only unique elements. Push both lists in a set and set will represent a list of all unique elements combined. This is two step process. Remove all elements of first list from second list, and then add first list to second list. How to merge two ArrayLists in Java. 1. Merge arraylists – List.addAll() method. addAll() method simplest way to append all of the elements in the given collection to the end of 2. Merge arraylists – Java 8 Stream.flatMap() 3. Merge two arraylists without duplicates.

You could use the Apache commons-collections library:

List<String> newList = ListUtils.union(list1, list2);

Concatenate Two ArrayList in Java, Just combine two lists with List.addAll() . JoinListsExample.java. package com.​mkyong.example; import java.util.ArrayList; import java.util.List  Here are the conditions: do not modify the original lists; JDK only, no external libraries.one-liner or a JDK 1.3 version is preferred. Is there a simpler way than: List<String> newList = new ArrayList<String>(); newList.addAll(listOne); newList.addAll(listTwo);

One of your requirements is to preserve the original lists. If you create a new list and use addAll(), you are effectively doubling the number of references to the objects in your lists. This could lead to memory problems if your lists are very large.

If you don't need to modify the concatenated result, you can avoid this using a custom list implementation. The custom implementation class is more than one line, obviously...but using it is short and sweet.

CompositeUnmodifiableList.java:

public class CompositeUnmodifiableList<E> extends AbstractList<E> {

    private final List<E> list1;
    private final List<E> list2;

    public CompositeUnmodifiableList(List<E> list1, List<E> list2) {
        this.list1 = list1;
        this.list2 = list2;
    }

    @Override
    public E get(int index) {
        if (index < list1.size()) {
            return list1.get(index);
        }
        return list2.get(index-list1.size());
    }

    @Override
    public int size() {
        return list1.size() + list2.size();
    }
}

Usage:

List<String> newList = new CompositeUnmodifiableList<String>(listOne,listTwo);

How can i return 2 ArrayList From same method, Example 1: Join Two Lists using addAll(). import java.util.ArrayList; import java.​util.List; public class JoinLists { public static void main(String[] args) { List<String>​  Both of the above conditions do not apply for the above case of joining two lists, so this is safe. Based on this answer to another question. Here's an approach using streams and java 8 if your lists have different types and you want to combine them to a list of another type.

Probably not simpler, but intriguing and ugly:

List<String> newList = new ArrayList<String>() { { addAll(listOne); addAll(listTwo); } };

Don't use it in production code... ;)

How to merge two ArrayLists in Java, Approach: ArrayLists can be joined in Java with the help of Collection.addAll() method. This method is called by the destination ArrayList and the other ArrayList​  In this tutorial we will see how to join (or Combine) two ArrayLists in Java. We will be using addAll() method to add both the ArrayLists in one final ArrayList. Example: In this example we are merging two ArrayLists in one single ArrayList and then displaying the elements of final List.

How to join two Lists in Java – Mkyong.com, A quick and practical guide to combining multiple collections in Java. and then you flatten the Stream before collecting it into a merged list: ? There are two main ways to merge, join or combine two List in Java, by using addAll(Collection c) method or by using ListUtils union method from Apache commons Collection. In this Java tutorial, we will see examples of both approaches.

Java Program to Join Two Lists, It can be used with different data types, and it accepts two source arrays along with the class literal to return the combined array. 4. Combining List  The new list should be made by splicing together the nodes of the first two lists. Java Solution. The key to solve the problem is defining a fake head. Then compare the first elements from each list. Add the smaller one to the merged list. Finally, when one of them is empty, simply append it to the merged list, since it is already sorted.

Join two ArrayLists in Java, In this example we are merging two ArrayLists in one single ArrayList and then displaying the elements of final List. package beginnersbook.com; import java.util​  There are various ways using which you can join two arrays in Java. 1) Join two arrays using the arraycopy method of the System class You can use the arraycopy method of System class to copy elements to a third array one by one.

Comments
  • If you are doing this solely for iteration purposes see another question - there are google guava and java 8 solutions stackoverflow.com/questions/4896662/…
  • Java 8 solution with utility method: stackoverflow.com/a/37386846/1216775
  • While you're technically correct, you have shortened it by one line, the asymmetry of this bugs me. Enough that I'm happier to "spend" the extra line.
  • Isn't there a problem here where the newList's interal array will be initialized to the size of listOne and then have to potentially expand when adding all of the items from listTwo? Would it be better to take the size of each list and use that to size the new array?
  • This was the solution that worked best for me. I made a comparison on the performance of different solutions this came out winner, together with creating the empty list and then addAll() of both. I tried all those that suggest not copying the lists and they result having a lot of overhead we didn't need this time.
  • nice but you can even make it shorter : List list = new ArrayList<>(list1).addAll(list2);
  • Gawd, that's a thing in Java 8? Technically you win I guess, but that's a heck of a long line :-)
  • I wonder if creating and running through two Streams has a noticeable performance impact. I suppose it shouldn't, compared to creating two Iterators.
  • For the casual reader, here is a shorter solution using also Java _ Streams: stackoverflow.com/a/34090554/363573
  • It is ugly but at least its fluent and can be used without multi line lambdas. I really wish there was a fluent addAll that returned the concatinated list.
  • I guess it's worth noting that it's really easy to get a distinct list out of this too, like so: List<String> newList = Stream.concat(listOne.stream(), listTwo.stream()).distinct().collect(Collectors.toList());
  • Nice, but requires apache commons. He did specify 'no external libraries'
  • @Quantum7, still useful for other people ;) Also, is apache commons even an external library? I don't start anything without it!