How to Join Arrays

JavaJavaBeginner
Practice Now

Introduction

In Java, it is often necessary to combine two or more arrays into a single array. This can be achieved using Java 8 stream API. In this lab, we will learn how to join multiple arrays into a single array using the Java code.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/generics("`Generics`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117438{{"`How to Join Arrays`"}} java/generics -.-> lab-117438{{"`How to Join Arrays`"}} java/oop -.-> lab-117438{{"`How to Join Arrays`"}} java/packages_api -.-> lab-117438{{"`How to Join Arrays`"}} java/wrapper_classes -.-> lab-117438{{"`How to Join Arrays`"}} java/identifier -.-> lab-117438{{"`How to Join Arrays`"}} java/arrays -.-> lab-117438{{"`How to Join Arrays`"}} java/comments -.-> lab-117438{{"`How to Join Arrays`"}} java/data_types -.-> lab-117438{{"`How to Join Arrays`"}} java/operators -.-> lab-117438{{"`How to Join Arrays`"}} java/output -.-> lab-117438{{"`How to Join Arrays`"}} java/strings -.-> lab-117438{{"`How to Join Arrays`"}} java/system_methods -.-> lab-117438{{"`How to Join Arrays`"}} end

Create the Java file

First, create a Java file named ArraysJoin.java in the ~/project/ directory by using the following command:

touch ~/project/ArraysJoin.java

Import Required Library

Import the required library for using Java 8 stream API.

import java.util.stream.Stream;

Define two Arrays

Define two arrays that you want to join. Here, we are taking two string arrays asia and europe.

String[] asia = new String[]{"India", "Russia", "Japan"};
String[] europe = new String[]{"Poland", "Germany", "France"};

Join Arrays using the Stream API

Now, initialize a new array that is a combination of both arrays using the Java 8 Stream API.

String[] countries = Stream.of(asia,europe).flatMap(Stream::of).toArray(String[]::new);

In the above example, the flatMap() method is used to merge two arrays and convert them into a stream of strings, and the toArray() method converts the stream into an array of strings.

Print the Joined Array

Finally, print the joined array elements using a for loop.

for (String country : countries) {
    System.out.println(country);
}

Concat Streams

We can also merge two streams by using the concat() method that returns a stream of specified type. The example shows how we can concatenate two or more streams into a single stream.

Stream<Integer> stream1 = Stream.of(1, 2, 3);
Stream<Integer> stream2 = Stream.of(4, 5, 6);
//concat arrays
Stream<Integer> result = Stream.concat(stream1, stream2);
result.forEach(System.out::println);

Compile the Java File

Compile the Java file using the following command:

javac ~/project/ArraysJoin.java

Run the Java Program

Run the Java program using the following command:

java -cp ~/project ArraysJoin

Output:

India
Russia
Japan
Poland
Germany
France
1
2
3
4
5
6

Summary

In this lab, we learned how to join multiple arrays into a single array using the Java 8 stream API. We also learned how to concatenate multiple streams into a single stream. The concept of joining and concatenating arrays and streams is used in programming when dealing with large amounts of data that needs to be stored in a single source platform.

Other Java Tutorials you may like