Java – How to Merge or Concatenate 2 Arrays

In this article, we will discuss how to merge or concatenate 2 Arrays of same type. In one of the previous article, we already discussed about merging 2 String[] Arrays but here we are going to discuss different approach.

Merging or Concatenating 2 Arrays:

Instead of simply merging by converting Arrays into different collection, we are going to discuss removing duplicates & sorting after merging

  • Merging 2 Arrays
  • Merging 2 Arrays and removing duplicates
  • Merging 2 Arrays and removing duplicates and sorting
  • Merging 2 Arrays and returning merged Array

1. Merging 2 Arrays:

  • There are 2 String[] Arrays defined and some names are repeated in both Arrays
  • We are going to convert 1st String[] Array into List, after creating List object of String type
  • Add 2nd String[] Array to List using addAll(); method
  • Now List contains all names from both String[] Arrays with duplicates as per insertion-order

MergeTwoArrays.java

package in.bench.resources.merge.arrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MergeTwoArrays {

	public static void main(String[] args) {

		// String[] array 1
		String[] names1 = {
				"Balazs",
				"Vikrant",
				"Mohasin",
				"Ahmed"
		};


		// print 1st String[] Array to console
		System.out.println("String[] Array 1 - \n" + Arrays.toString(names1));


		// String[] array 1
		String[] names2 = {
				"Vihar",
				"Balazs",
				"Roshan",
				"Vikrant"
		};


		// print 2nd String[] Array to console
		System.out.println("\nString[] Array 2 - \n" + Arrays.toString(names2));


		// convert first String[] Array to List
		List<String> names = new ArrayList<String>(Arrays.asList(names1));


		// add second String[] Array to List using addAll(); method
		names.addAll(Arrays.asList(names2));


		// print merged String[] Array to console
		System.out.println("\nAfter merging both String[] Array - \n" + names);
	}
}

Output:

String[] Array 1 - 
[Balazs, Vikrant, Mohasin, Ahmed]

String[] Array 2 - 
[Vihar, Balazs, Roshan, Vikrant]

After merging both String[] Array - 
[Balazs, Vikrant, Mohasin, Ahmed, Vihar, Balazs, Roshan, Vikrant]

2. Merging 2 Arrays and removing duplicates:

  • There are 2 String[] Arrays defined and some names are repeated in both Arrays
  • We are going to convert 1st String[] Array into Set, after creating Set object of String type
  • Add 2nd String[] Array to Set using addAll(); method
  • Now Set contains all unique names from both String[] Arrays after removing duplicates in random-order

MergeTwoArraysAndRemoveDuplicates.java

package in.bench.resources.merge.arrays;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MergeTwoArraysAndRemoveDuplicates {

	public static void main(String[] args) {

		// String[] array 1
		String[] names1 = {
				"Balazs",
				"Vikrant",
				"Mohasin",
				"Ahmed"
		};


		// print 1st String[] Array to console
		System.out.println("String[] Array 1 - \n" + Arrays.toString(names1));


		// String[] array 1
		String[] names2 = {
				"Vihar",
				"Balazs",
				"Roshan",
				"Vikrant"
		};


		// print 2nd String[] Array to console
		System.out.println("\nString[] Array 2 - \n" + Arrays.toString(names2));


		// convert first String[] Array to Set
		Set<String> names = new HashSet<String>(Arrays.asList(names1));


		// add second String[] Array to Set using addAll(); method
		names.addAll(Arrays.asList(names2));


		// print merged/unique String[] Array to console
		System.out.println("\nAfter merging and removing duplicates from both String[] Array - \n" 
				+ names);
	}
}

Output:

String[] Array 1 - 
[Balazs, Vikrant, Mohasin, Ahmed]

String[] Array 2 - 
[Vihar, Balazs, Roshan, Vikrant]

After merging and removing duplicates from both String[] Array - 
[Vikrant, Roshan, Ahmed, Balazs, Vihar, Mohasin]

3. Merging 2 Arrays and removing duplicates and sorting:

  • There are 2 String[] Arrays defined and some names are repeated in both Arrays
  • We are going to convert 1st String[] Array into Set, after creating Set object of String type
  • Add 2nd String[] Array to Set using addAll(); method
  • Now Set contains all unique names from both String[] Arrays after removing duplicates in random-order
  • For sorting, convert Set into List using inter-conversion constructor
  • Finally, we will be sorting the List elements using Collections.sort(list); to print names to console in alphabetical-order

MergeTwoArraysRemoveDuplicatesAndSort.java

package in.bench.resources.merge.arrays;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MergeTwoArraysRemoveDuplicatesAndSort {

	public static void main(String[] args) {

		// String[] array 1
		String[] names1 = {
				"Balazs",
				"Vikrant",
				"Mohasin",
				"Ahmed"
		};


		// print 1st String[] Array to console
		System.out.println("String[] Array 1 - \n" + Arrays.toString(names1));


		// String[] array 1
		String[] names2 = {
				"Vihar",
				"Balazs",
				"Roshan",
				"Vikrant"
		};


		// print 2nd String[] Array to console
		System.out.println("\nString[] Array 2 - \n" + Arrays.toString(names2));


		// convert first String[] Array to Set
		Set<String> names = new HashSet<String>(Arrays.asList(names1));


		// add second String[] Array to Set using addAll(); method
		names.addAll(Arrays.asList(names2));


		// convert Set to List for sorting
		List<String> list = new ArrayList<String>(names);


		// sorting names in alphabetical order
		Collections.sort(list);


		// print merged/unique String[] Array in ascending order
		System.out.println("\nSorted String[] Array - After merging and removing duplicates - \n" 
				+ list);
	}
}

Output:

String[] Array 1 - 
[Balazs, Vikrant, Mohasin, Ahmed]

String[] Array 2 - 
[Vihar, Balazs, Roshan, Vikrant]

Sorted String[] Array - After merging and removing duplicates - 
[Ahmed, Balazs, Mohasin, Roshan, Vihar, Vikrant]

4. Merging 2 Arrays and returning merged Array:

  • There are 2 String[] Arrays defined and some names are repeated in both Arrays
  • A static method “mergeArrays” defined to accept 2 String[] Arrays as arguments and returns single merged String[] Array
  • Inside this method,
    • We are going to convert 1st String[] Array into Set, after creating Set object of String type
    • Add 2nd String[] Array to Set using addAll(); method
  • Now Set contains all unique names from both String[] Arrays after removing duplicates in random-order
  • Finally, convert Set into Array using toArray(); method passing “new String[0]” as argument and return single merged String[] Array to calling method

MergeTwoArraysAndReturnArray.java

package in.bench.resources.merge.arrays;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MergeTwoArraysAndReturnArray {

	public static void main(String[] args) {

		// String[] array 1
		String[] names1 = {
				"Balazs",
				"Vikrant",
				"Mohasin",
				"Ahmed"
		};


		// print 1st String[] Array to console
		System.out.println("String[] Array 1 - \n" + Arrays.toString(names1));


		// String[] array 1
		String[] names2 = {
				"Vihar",
				"Balazs",
				"Roshan",
				"Vikrant"
		};


		// print 2nd String[] Array to console
		System.out.println("\nString[] Array 2 - \n" + Arrays.toString(names2));


		// invoke mergeArrays() method
		String[] mergedArray = mergeArrays(names1, names2);


		// print merged String[] Array to console
		System.out.println("\nAfter merging both String[] Array - \n" 
				+ Arrays.toString(mergedArray));
	}

	public static String[] mergeArrays(String[] strArray1, String[] strArray2) {

		// convert first String[] Array to Set
		Set<String> names = new HashSet<String>(Arrays.asList(strArray1));


		// add second String[] Array to Set using addAll(); method
		names.addAll(Arrays.asList(strArray2));


		// return String[]
		return names.toArray(new String[0]);
	}
}

Output:

String[] Array 1 - 
[Balazs, Vikrant, Mohasin, Ahmed]

String[] Array 2 - 
[Vihar, Balazs, Roshan, Vikrant]

After merging both String[] Array - 
[Vikrant, Roshan, Ahmed, Balazs, Vihar, Mohasin]

Hope, everyone found this article very useful while converting multiple Arrays into single Array. In the following article, we are going to repeat same examples/concepts using Java 8 Stream APIs.

Happy Coding !!
Happy Learning !!

Java – Find sum of Largest 2 numbers in an Arrays or List
Java 8 - How to store multiple values for single key in HashMap ?