Java8 Concatenate Arrays Example | Java Concatenate Two Arrays

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, I am going to show you how to concatenate arrays using Java8 streams.
import java.util.Arrays;
import java.util.stream.Stream;

public class Array_Concatinate {

    public static void main(String[] args) {

        String[] alphabets = { "AB", "BA", "AC" };
        String[] numarics = { "1", "2", "3" };

        String[] both = Stream.concat(Arrays.stream(alphabets),
                Arrays.stream(numarics)).toArray(String[]::new);
        for (String string : both) {
            System.out.println(string);
        }
    }
}
Output:
AB
BA
AC
1
2
3
import java.util.stream.Stream;

public class Array_Concatinate {

    public static void main(String[] args) {

        String[] alphabets = { "AB", "BA", "AC" };
        String[] numarics = { "1", "2", "3" };

        String[] both = Stream.of(alphabets, numarics).flatMap(Stream::of)
                .toArray(String[]::new);

        for (String string : both) {
            System.out.println(string);
        }
    }
}
[/java]
Output:
AB
BA
AC
1
2
3
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Array_Concatinate {

    public static void main(String[] args) {

        String[] alphabets = { "AB", "BA", "AC" };
        String[] numarics = { "1", "2", "3" };

        List<String> both = new ArrayList<String>(alphabets.length
                + numarics.length);
        Collections.addAll(both, alphabets);
        Collections.addAll(both, numarics);
        String[] strArray = both.toArray(new String[both.size()]);

        for (String string : strArray) {
            System.out.println(string);
        }
    }
}
Output:
AB
BA
AC
1
2
3
Happy Learning 🙂

User defined sorting with Java 8 Comparator
Java 8 Read File Line By Line Example
How to Filter a Map in Java 8
How to Filter null values from Java8 Stream
Java 8 Array contains a specific value ?
Java 8 groupingBy Example
How to sort a Map using Java8
Java 8 Stream Filter Example with Objects
Java 8 How to convert Stream to List
How to get Stream count in Java 8
Java 8 Getting Min and Max values from a Stream
How to Convert Iterable to Stream Java 8
How Java 8 Stream generate random String and Numbers
How to Install Java8 on Windows 10
Java 8 Stream API and Parallelism

User defined sorting with Java 8 Comparator
Java 8 Read File Line By Line Example
How to Filter a Map in Java 8
How to Filter null values from Java8 Stream
Java 8 Array contains a specific value ?
Java 8 groupingBy Example
How to sort a Map using Java8
Java 8 Stream Filter Example with Objects
Java 8 How to convert Stream to List
How to get Stream count in Java 8
Java 8 Getting Min and Max values from a Stream
How to Convert Iterable to Stream Java 8
How Java 8 Stream generate random String and Numbers
How to Install Java8 on Windows 10
Java 8 Stream API and Parallelism
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String