7

Using Java 8 I am trying to concatenate two float arrays:

void f(float[] first, float[] second) {
    float[] both = ???
}

From a quick SO search, I thought I could simply follow instruction from here. So I tried:

float both[] = FloatStream.concat(Arrays.stream(first), Arrays.stream(second)).toArray();

But this does not compile as explained here. So I tried the less efficient solution and use a Stream directly:

float[] both = Stream.concat(Arrays.stream(first), Arrays.stream(second)).toArray(float[]::new);

It fails to compile from my eclipse saying:

The method stream(T[]) in the type Arrays is not applicable for the arguments  (float[])

What is the most efficient (and simple) way of concatenating two float[] arrays in Java 8 ?


Update: obviously the whole point of the question is that I have to deal with float and not double.

malat
  • 12,152
  • 13
  • 89
  • 158
  • 1
    If you can use a `double[]` instead of `float[]`, `double[] both = DoubleStream.concat(Arrays.stream(first), Arrays.stream(second)).toArray();` will work. – Eran Oct 25 '17 at 10:02

4 Answers4

6

Don't do it yourself, use System.arrayCopy() to copy both arrays into a new array of the combined size. That's much more efficient, as it uses native OS code.

Sandip Solanki
  • 704
  • 1
  • 8
  • 15
4

Since there is no FloatStream and even creating an (inefficient) boxed stream out of a float array is not simple, you won’t find a stream based solution that is simpler than

static float[] f(float[] first, float[] second) {
    float[] both = Arrays.copyOf(first, first.length+second.length);
    System.arraycopy(second, 0, both, first.length, second.length);
    return both;
}

Even the option to use a parallel stream is unlikely to outweigh the raised complexity. You would need really large arrays to see a benefit.

Holger
  • 285,553
  • 42
  • 434
  • 765
  • 1
    I was about to update the [other answer](https://stackoverflow.com/a/46929463/1746118) with a bit longer version of the same. Nice optimization overall though. – Naman Oct 25 '17 at 10:18
2

Don't write your own code to do this. The problem has already been solved and tested. You should use the Apache Commons ArrayUtils class.

float[] both = ArrayUtils.addAll(first, second);

Under the covers, it has some logic for a couple of special cases where one input or the other is null, followed by two calls to System.arraycopy. But you don't need to worry about any of that.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • In my opinion this is the correct answer to the question, a oneliner which effectively solves the problem. – waykiki Nov 25 '22 at 13:38
0

is guava an option? since it has Floats.concat a bit more fluent, but it's still System.arrayCopy underneath.

Problem is that jdk does not have a FloatStream - that could be used here, you might return a Float[] from such a thing:

Stream.concat(
            IntStream.range(0, first.length)
                    .boxed()
                    .map(x -> first[x]),
            IntStream.range(0, first.length)
                    .boxed()
                    .map(y -> first[y]))
            .toArray(Float[]::new);

But that is too many operations for such a simple thing.

Eugene
  • 117,005
  • 15
  • 201
  • 306