java

How to Merge Two Arrays in Java

In Java, there are several ways to merge or add two arrays: with Java home resources prior to Java 8, with Java 8 streams, or with the help of the Guava or Apache Commons libraries.
 

Merge two arrays in Java

To merge two arrays into one, we use two methods of the Java Standard Edition: Arrays.copyOf() and System.arraycopy(). Arrays.copyOf() creates a new array result with the contents of the first array one, but with the length of both arrays. System.arraycopy() then does the real work of copying: it copies the second array into the result array just created with the length of both arrays.
 

 

Example :
import java.util.Arrays;

public class MergeArrays 
{
    public static void main(String[] args) 
    {
        String[] birds = {"Angel", "Buddy", "Sunny", "Sunshine"};
        String[] cats = {"Lions", "Tigers", "Leopards", "Cheetahs"};
        String[] animals = concatArrays(birds, cats);
        System.out.println(Arrays.toString(animals));
    }

    public static <T> T[] concatArrays(T[] arr1, T[] arr2) 
    {
        T[] result = Arrays.copyOf(arr1, arr1.length + arr2.length);
        System.arraycopy(arr2, 0, result, arr1.length, arr2.length);
        return result;
    }
}

Output:

[Angel, Buddy, Sunny, Sunshine, Lions, Tigers, Leopards, Cheetahs]

Why the method copyOf() is in the Util class Arrays, but the method arraycopy() in the class System, is illogical. The reason should be quite simply historical: the System class has been around since Java 1.0, the Arrays class only since Java 1.2.
java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – OOPsThis collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java OOPs”.   1. Which of the…Read More

 

Merge two arrays in Java with Java 8 streams

In Java 8, the API was extended to include streams, which represent an elegant declarative fluent interface for processing collections.

Arrays can be converted into a stream quite easily using Arrays.stream(). This stream contains all elements of the array.
 

Example :
import java.util.Arrays;
import java.util.stream.Stream;

public class MergeArrays 
{
    public static void main(String[] args) 
    {
        String[] birds = {"Angel", "Buddy", "Sunny", "Sunshine"};
        String[] cats = {"Lions", "Tigers", "Leopards", "Cheetahs"};
        
        String[] animals = Stream.concat(
                Arrays.stream(birds), 
                Arrays.stream(cats)
            ).toArray(String[]::new);
            
        System.out.println(Arrays.toString(animals));
    }
}

Output:

[Angel, Buddy, Sunny, Sunshine, Lions, Tigers, Leopards, Cheetahs]

Stream.concat() creates a new stream containing the elements of the first stream before the elements of the second stream. The method toArray() converts the stream back into an array.
Difference between == and .equalsDifference between == and .equals()In short: .equals() is used to compare objects, and the equal-to operator (==) is used to compare references and simple types such as int and…Read More

 

Merge two arrays in Java with Google Guava

Google Guava offers an easy way to merge two arrays with the ObjectArrays.concat() method.

import java.util.Arrays;
import com.google.common.collect.ObjectArrays;

public class MergeArrays 
{
    public static void main(String[] args) 
    {
        String[] birds = {"Angel", "Buddy", "Sunny", "Sunshine"};
        String[] cats = {"Lions", "Tigers", "Leopards", "Cheetahs"};
        String[] animals = ObjectArrays.concat(birds, cats, String.class);
        System.out.println(Arrays.toString(animals));
    }
}

Output:

[Angel, Buddy, Sunny, Sunshine, Lions, Tigers, Leopards, Cheetahs]
 

Merge two arrays in Java with Apache Commons Lang ArrayUtils

Apache Commons also provides a method for merging arrays in the ArrayUtils class:

import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;

public class MergeArrays 
{
    public static void main(String[] args) 
    {
        String[] birds = {"Angel", "Buddy", "Sunny", "Sunshine"};
        String[] cats = {"Lions", "Tigers", "Leopards", "Cheetahs"};
        String[] animals;

        animals = ArrayUtils.addAll(birds, cats);
        
        System.out.println(Arrays.toString(animals));
    }
}

Output:

[Angel, Buddy, Sunny, Sunshine, Lions, Tigers, Leopards, Cheetahs]
java-mcq-multiple-choice-questions-and-answersJava MCQ – Multiple Choice Questions and Answers – Array – Part 1This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Array”.   1. Array is a…Read More mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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