What does "String[]::new" mean?

Viewed 10192

I'm learning how to use stream, and I get a problem with this method.

public static String[] inArray(String[] array1, String[] array2) {
   return Arrays.stream(array1)
  .filter(str -> Arrays.stream(array2).anyMatch(s -> s.contains(str)))
  .distinct().sorted().toArray(**String[]::new**);
}

I'm so confused about String[]::new, could you give me a hint?

10 Answers

I would say the existing answers provide some insight but none of them yet talk about IntFunction<R>.

To add to them explain, what it means in the context of Stream.toArray(String[]::new) is that it represents an IntFunction implementation such as :

new IntFunction<String[]>() {
    @Override
    public String[] apply(int value) {
        return new String[value];
    }
}

where the code creates a newly allocated String[] of size value and produces the array of that size as an output.

You are right to be confused, because Java isn't really super clear about types vs. classes.

We know that String[] is a type, as you can declare variables of that type:

jshell> String[] s = new String[]{"Hello", "world"}
s ==> String[2] { "Hello", "world" }

However, String[] actually is treated as a class in Java and not just a type:

jshell> s.getClass()
$2 ==> class [Ljava.lang.String;

That funny looking [Ljava.lang.String, representing the type "array of string" shows up in response to the getClass invocation. I agree that it is surprising. But every object in Java has to have a class, and String[] is that class. (In other languages, you might see something like Array<String> which might be a dash clearer. But then Java has type erasure so again, things look a little confusing.)

In your particular case, here's what's going on. You need to be careful with types when making arrays from streams. Naively, you might get:

jshell> Arrays.asList("a", "b").stream().toArray()
$5 ==> Object[2] { "a", "b" }

So we want the version of toArray that gives us an array:

jshell> Arrays.asList("a", "b").stream().toArray((n) -> new String[n])
$7 ==> String[2] { "a", "b" }

That's better! The result type is an array of strings, instead of just an array of obejcts. Now the (n)->new String[n] can be replaced with a method reference for construction. Java allows array types in method references! So we can write:

jshell> Arrays.asList("a", "b").stream().toArray(String[]::new)
$8 ==> String[2] { "a", "b" }

Aside: There are some caveats when using array types in method references like this, such as the requirement that the array type must be reifiable, but I think that's a little beyond what you might have been asking. The TL;DR here is that, by design, Java allows array types in (constructor-like) method references with ::new.

This is a method reference expression see JLS 15.13. The syntax for method references is:

MethodReference:
    ExpressionName :: [TypeArguments] Identifier
    Primary :: [TypeArguments] Identifier
    ReferenceType :: [TypeArguments] Identifier
    super :: [TypeArguments] Identifier
    TypeName . super :: [TypeArguments] Identifier
    ClassType :: [TypeArguments] new
    ArrayType :: new 

The particular case you are looking at is the last one. In your example, String[] is an ArrayType which means that it consists of a type name followed by one or more [].

There shouldn't be a class named String[] which is very lame and I could not interpret what it is actually meant for.

See above: it is a type specification not a class name. From a syntactic / linguistic perspective, this usage is analogous to:

  Class<?> c = String[].class;

or

  if (a instanceof String[])

or even

  public void myMethod(String[] arg)

(You wouldn't call those "lame" ... would you?)


Now you could have a valid case for saying that it is syntactically unexpected (especially to a pre-Java 8 programmer) to be able to use the new keyword like this. But this unexpected syntax is a consequence of the strong imperative that the designers have to NOT break backwards compatibility when adding new language features to Java. And it is not unintuitive. (At least, I don't think so. When I first saw this construct, is was obvious to me what it meant.)

Now, if they were starting with a clean slate in 2018, a lot of details of the Java language design would be simpler and cleaner. But they don't have the luxury of doing that.

String[]::new

This is lambda for the following method:

public String[] create(int size) {
    return new String[size];
}

Adding to the answer of Andrew Tobilko:

"String[]::new means size -> new String[size]"

which, since toArray takes an IntFunction, is similar to:

IntFunction<String[]> generator = new IntFunction<String[]>() {
    @Override
    public String[] apply(int size) {
        return new String[size];
    }
};
Related