In this article, we will discuss how to merge or concatenate 2 Arrays of same type using Java 8 Stream API. Already in the previous articles we have discussed about merging/concatenating 2 Arrays using different approaches,
Java 8 Stream API – Merging 2 Arrays:
Instead of simply merging using Stream API, we are going to discuss removing duplicates & sorting after merging,
- Merging 2 Arrays
- Merging 2 Arrays using Stream.of() method
- Concatenating 2 Arrays using Stream.concat() method
- 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:
- We are going to discuss 2 different approaches of merging/concatenating 2 Arrays using Stream API i.e.,
- Using Stream.of() method
- Using Stream.concat() method
1.1 Merging 2 Arrays using Stream.of() method
- There are 2 String[] Arrays defined and some names are repeated in both Arrays
- Stream.of() – this method helps to merge 2 or more Arrays by passing those Arrays as input arguments
- Stream.flatMap() – this method helps to flattens the multiple Array structure into single Array
- Stream.toArray() – this method converts Stream into Array by passing Constructor Reference “String[]::new” as argument
MergeTwoArraysUsingJava8StreamOf.java
package in.bench.resources.merge.arrays;
import java.util.Arrays;
import java.util.stream.Stream;
public class MergeTwoArraysUsingJava8StreamOf {
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));
// Merge or Concatenate 2 Arrays using Stream.of()
String[] mergedNames = Stream
.of(names1, names2) // merging
.flatMap(Stream::of) // flattening
.toArray(String[]::new); // converting to Array
// print merged String[] Array to console
System.out.println("\nAfter merging both String[] Arrays - \n"
+ Arrays.toString(mergedNames));
}
}
Output:
String[] Array 1 -
[Balazs, Vikrant, Mohasin, Ahmed]
String[] Array 2 -
[Vihar, Balazs, Roshan, Vikrant]
After merging both String[] Arrays -
[Balazs, Vikrant, Mohasin, Ahmed, Vihar, Balazs, Roshan, Vikrant]
1.2 Concatenating 2 Arrays using Stream.concat() method
- There are 2 String[] Arrays defined and some names are repeated in both Arrays
- Stream.concat() – this method helps to merge 2 or more Arrays by passing those Arrays as input arguments
- Stream.toArray() – this method converts Stream into Array by passing Constructor Reference “String[]::new” as argument
MergeTwoArraysUsingJava8StreamConcat.java
package in.bench.resources.merge.arrays;
import java.util.Arrays;
import java.util.stream.Stream;
public class MergeTwoArraysUsingJava8StreamConcat {
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));
// Merge or Concatenate 2 Arrays using Stream.concat()
String[] mergedNames = Stream
.concat(Arrays.stream(names1), Arrays.stream(names2)) // merging & flattening
.toArray(String[]::new); // converting to Array
// print merged String[] Array to console
System.out.println("\nAfter merging both String[] Arrays - \n"
+ Arrays.toString(mergedNames));
}
}
Output:
String[] Array 1 -
[Balazs, Vikrant, Mohasin, Ahmed]
String[] Array 2 -
[Vihar, Balazs, Roshan, Vikrant]
After merging both String[] Arrays -
[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
- Stream.of() – this method helps to merge 2 or more Arrays by passing those Arrays as input arguments
- Stream.flatMap() – this method helps to flattens the multiple Array structure into single Array
- Stream.distinct() – this method removes duplicate elements from Stream
- Stream.toArray() – this method converts Stream into Array by passing Constructor Reference “String[]::new” as argument
MergeTwoArraysAndRemoveDuplicatesUsingJava8.java
package in.bench.resources.merge.arrays;
import java.util.Arrays;
import java.util.stream.Stream;
public class MergeTwoArraysAndRemoveDuplicatesUsingJava8 {
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));
// Merge or Concatenate 2 Arrays using Stream.of()
String[] mergedNames = Stream
.of(names1, names2) // merging
.flatMap(Stream::of) // flattening
.distinct() // removes duplicates
.toArray(String[]::new); // converting to Array
// print merged/unique String[] Array to console
System.out.println("\nAfter merging and removing duplicates from both String[] Arrays - \n"
+ Arrays.toString(mergedNames));
}
}
Output:
String[] Array 1 -
[Balazs, Vikrant, Mohasin, Ahmed]
String[] Array 2 -
[Vihar, Balazs, Roshan, Vikrant]
After merging and removing duplicates from both String[] Arrays -
[Balazs, Vikrant, Mohasin, Ahmed, Vihar, Roshan]
3. Merging 2 Arrays and removing duplicates and sorting:
- There are 2 String[] Arrays defined and some names are repeated in both Arrays
- Stream.of() – this method helps to merge 2 or more Arrays by passing those Arrays as input arguments
- Stream.flatMap() – this method helps to flattens the multiple Array structure into single Array
- Stream.distinct() – this method removes duplicate elements from Stream
- Stream.sorted() – this method sorts Stream elements in natural alphabetical-order
- Stream.toArray() – this method converts Stream into Array by passing Constructor Reference “String[]::new” as argument
MergeTwoArraysAndRemoveDuplicatesAndSortingUsingJava8.java
package in.bench.resources.merge.arrays;
import java.util.Arrays;
import java.util.stream.Stream;
public class MergeTwoArraysAndRemoveDuplicatesAndSortingUsingJava8 {
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));
// Merge or Concatenate 2 Arrays using Stream.of()
String[] mergedNames = Stream
.of(names1, names2) // merging
.flatMap(Stream::of) // flattening
.distinct() // removes duplicates
.sorted() // sorting in alphabetical order
.toArray(String[]::new); // converting to Array
// print merged/unique String[] Array in ascending order
System.out.println("\nSorted String[] Array - After merging and removing duplicates - \n"
+ Arrays.toString(mergedNames));
}
}
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:
- This illustration is very much similar to 2nd example except that it returns Array from method after conversion
- From main() method, we are invoking another method called “mergeArrays()” for converting 2 Arrays into single merged Array by removing duplicates and then returning merged Array
- Inside method we are doing,
- Stream.of() – this method helps to merge 2 or more Arrays by passing those Arrays as input arguments
- Stream.flatMap() – this method helps to flattens the multiple Array structure into single Array
- Stream.distinct() – this method removes duplicate elements from Stream
- Stream.toArray() – this method converts Stream into Array by passing Constructor Reference “String[]::new” as argument
- Finally, printing merged Array elements with unique names to the console
MergeTwoArraysAndReturnArrayUsingJava8.java
package in.bench.resources.merge.arrays;
import java.util.Arrays;
import java.util.stream.Stream;
public class MergeTwoArraysAndReturnArrayUsingJava8 {
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[] mergedNames = mergeArrays(names1, names2);
// print merged/unique String[] Array to console
System.out.println("\nAfter merging and removing duplicates from both String[] Arrays - \n"
+ Arrays.toString(mergedNames));
}
// method to merge both Arrays
public static String[] mergeArrays(String[] strArray1, String[] strArray2) {
// Merge or Concatenate 2 Arrays using Stream.of()
String[] mergedNames = Stream
.of(strArray1, strArray2) // merging
.flatMap(Stream::of) // flattening
.distinct() // removes duplicates
.toArray(String[]::new); // converting to Array
// return
return mergedNames;
}
}
Output:
String[] Array 1 -
[Balazs, Vikrant, Mohasin, Ahmed]
String[] Array 2 -
[Vihar, Balazs, Roshan, Vikrant]
After merging and removing duplicates from both String[] Arrays -
[Balazs, Vikrant, Mohasin, Ahmed, Vihar, Roshan]
Hope, everyone found this article very useful while converting multiple Arrays into single Array using Java 8 Stream APIs
Related Articles:
- Java 8 – Merging two or more Stream of elements
- Java – Merging 2 Arrays using List/Set approach
- Java – Concatenating 2 Arrays using Third Arrays approach
- Java 8 – How to Merge or Concatenate 2 Arrays using Stream API ?
Happy Coding !!
Happy Learning !!