forked from nrkapri/JavaAdvancedTopics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinArraysExample.java
More file actions
37 lines (25 loc) · 1.07 KB
/
Copy pathJoinArraysExample.java
File metadata and controls
37 lines (25 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package java8;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class JoinArraysExample {
public static void main (String [] args)
{
String[] s1 = new String[]{"a", "b", "c"};
String[] s2 = new String[]{"d", "e", "f"};
String[] s3 = new String[]{"g", "h", "i"};
//join object type array
String[] result = Stream.of(s1, s2, s3).flatMap(Stream::of).toArray(String[]::new);
System.out.println(Arrays.toString(result));
int [] int1 = new int[]{1,2,3};
int[] int2 = new int[]{4,5,6};
int[] int3 = new int[]{7,8,9};
//join 2 primitive type array
int[] result2 = IntStream.concat(Arrays.stream(int1), Arrays.stream(int2)).toArray();
//join 3 primitive type array, any better idea?
int[] result3 = IntStream.concat(Arrays.stream(int1),
IntStream.concat(Arrays.stream(int2), Arrays.stream(int3))).toArray();
System.out.println(Arrays.toString(result2));
System.out.println(Arrays.toString(result3));
}
}