5

This page shows how to combine two arrays of Integer objects into an array of Object objects.

Integer[] firstArray = new Integer[] { 10 , 20 , 30 , 40 };
Integer[] secondArray = new Integer[] { 50 , 60 , 70 , 80 };
Object[] merged = 
        Stream
        .of( firstArray , secondArray )
        .flatMap( Stream :: of )
        .toArray()
;

Arrays.toString( merged ): [10, 20, 30, 40, 50, 60, 70, 80]

➥ Is there a way to use Java streams to concatenate a pair of arrays of primitive int values rather than objects?

int[] a = { 10 , 20 , 30 , 40 };
int[] b = { 50 , 60 , 70 , 80 };
int[] merged = … ?

I realize using Java streams may not be the most efficient way to go. But I am curious about the interplay of primitives and Java streams.

I am aware of IntStream but cannot see how to use it for this purpose.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154

4 Answers4

14

IntStream.concat

Transform each array into an IntStream. Then call IntStream.concat to combine.

Lastly, generate an array of int by calling IntStream::toArray.

int[] a = { 10 , 20 , 30 , 40 };
int[] b = { 50 , 60 , 70 , 80 };

int[] merged = IntStream.concat(IntStream.of(a), IntStream.of(b)).toArray();

System.out.println(Arrays.toString(merged));

See this code run live at IdeOne.com.

Output:

[10, 20, 30, 40, 50, 60, 70, 80]

Tip: To sort the results, call .sorted() before the .toArray(). As seen running on IdeOne.com.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Denis Zavedeev
  • 7,627
  • 4
  • 32
  • 53
  • This seems to be the most efficient of all these interesting Answers, as it is the only one to avoid the overhead of [auto-boxing](https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html). Anyone, please correct me if I am misunderstanding the boxing involved in such work. – Basil Bourque Apr 18 '20 at 22:50
  • 1
    @BasilBourque quick test: put a breakpoint at [`Integer#valueOf`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Integer.html#valueOf(int)), run debugger and see if it is triggered. It did not:) on openjdk version "11.0.6" 2020-01-14 – Denis Zavedeev Apr 18 '20 at 23:25
  • @BasilBourque Just a side note, that [the other variant](https://stackoverflow.com/a/54864499/1746118) is still slightly better [than this](https://stackoverflow.com/a/54864606/1746118) in one way. – Naman Apr 19 '20 at 04:06
3

Here is one way, using Stream::flatMapToInt.

int[] a = { 10 , 20 , 30 , 40 };
int[] b = { 50 , 60 , 70 , 80 };
int[] merged = Stream.of(a,b).flatMapToInt(IntStream::of)
                .toArray();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
WJS
  • 36,363
  • 4
  • 24
  • 39
  • Interesting. I had not noticed [`Stream::flatMapToInt`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/Stream.html#flatMapToInt(java.util.function.Function)). But I suspect the [Answer by caco3](https://stackoverflow.com/a/61297134/642706) would avoid auto-boxing and therefore be more performant. – Basil Bourque Apr 18 '20 at 22:44
  • Yep. And I forgot about `IntStream.concat()`. One of these days I may get this all down. – WJS Apr 18 '20 at 22:45
3

You could use something like that if you want to work with merged:

Integer[] firstArray = new Integer[] {10, 20, 30, 40};
Integer[] secondArray = new Integer[] {50, 60, 70, 80};
Object[] merged = Stream.of(firstArray, secondArray).flatMap(Stream::of).toArray();

int[] ints = Arrays.stream(merged).mapToInt(o -> Integer.parseInt(o.toString())).toArray();
System.out.println(Arrays.toString(ints));

Output:

[10, 20, 30, 40, 50, 60, 70, 80]
Jerome Wolff
  • 356
  • 3
  • 19
2

You can use concat method of Stream to merge them and use boxed method of IntStream to convert it to Stream.

int[] firstArray = new int[]{10, 20, 30, 40};
int[] secondArray = new int[]{50, 60, 70, 80};
Object[] merged =
        Stream
              .concat(Arrays.stream(firstArray).boxed(), Arrays.stream(secondArray).boxed())
              .toArray();

System.out.println(Arrays.toString(merged)); // [10, 20, 30, 40, 50, 60, 70, 80]
SMortezaSA
  • 589
  • 2
  • 15
  • Worthy of an up-vote, but it looks like the [Answer by caco3](https://stackoverflow.com/a/61297134/642706) does much the same while avoiding the overhead of auto-boxing. – Basil Bourque Apr 18 '20 at 22:47