Home > Tutorial > Java ArrayList Combine Example

Java ArrayList Combine Example

If you have one or more ArrayList objects that you need to combine or merge into a single ArrayList object, it can be easily done in Java. The ArrayList.addAll() method will append all of the elements in the specified list to the end of this ArrayList.

The other ArrayList.addAll() method will also append the contents of one list to another, but it will also allow you to define where in the list you would like to append to. This additional parameter is defined as the index. For example, instead of adding elements to the end of the list you can add them to the beginning or even in the middle.

  • addAll(Collection c)
  • addAll(int index, Collection c)


Java ArrayList Append End

By Using the addAll(arg1) method with just the collection as an argument, it will append the contents of jcdListA to the end of jcdListCombined.

//Let's combine jcdListA to the end of jcdListCombined
jcdListCombined.addAll(jcdListA);


Java ArrayList Pre Append

By Using the addAll(arg1, arg2) method with the index and the collection as an argument, it will append the contents of jcdListC to jcdListCombined starting at the position identified in arg1.

//Let's combine jcdListC to the beginning of jcdListCombined
jcdListCombined.addAll(0, jcdListC);


package com.jcd.java.tutorials;

import java.util.ArrayList;

public class JavaArrayListCombine 
{

	public static void main(String[] args) 
	{
		//Let's create a new ArrayList object
		ArrayList<String> jcdListA = new ArrayList<String>();
		
		//Add some elements to this ArrayList
		jcdListA.add("Java");
		jcdListA.add("Code");
		jcdListA.add("Depot");
		jcdListA.add("Is An");
		
		//Let's create another ArrayList object
		ArrayList<String> jcdListB = new ArrayList<String>();
		
		//Add some elements to this ArrayList
		jcdListB.add("Online");
		jcdListB.add("Resource");
		jcdListB.add("For");
		jcdListB.add("Java");
		jcdListB.add("Programmers");

		//Let's create another ArrayList object
		ArrayList<String> jcdListC = new ArrayList<String>();
		jcdListC.add("FACTS:");
		
		//Let's create another ArrayList object that will be for combining
		ArrayList<String> jcdListCombined = new ArrayList<String>();
		
		
		//Let's combine jcdListA to the end of jcdListCombined
		jcdListCombined.addAll(jcdListA);
		
		//Let's combine jcdListB to the end of jcdListCombined
		jcdListCombined.addAll(jcdListB);
		
		//Let's combine jcdListC to the beginning of jcdListCombined
		jcdListCombined.addAll(0, jcdListC);
		
		//Print out our combined ArrayList with enhanced for loop
		for(String str : jcdListCombined) 
		{
			System.out.println(str);
		}
		
		System.out.println("\n"+jcdListCombined.toString());
	}

}


FACTS:
Java
Code
Depot
Is An
Online
Resource
For
Java
Programmers

[FACTS:, Java, Code, Depot, Is An, Online, Resource, For, Java, Programmers]



Concluding Comments

The ArrayList.addAll() method makes combining different ArrayList objects into a single ArrayList object. It’s pretty easy to append a list to pretty much any location of another list.



Java References:

Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2018 JavaCodeDepot