Java stream toArray() convert to a specific type of array

后端 未结 2 1502
轮回少年
轮回少年 2020-12-08 01:43

Maybe this is very simple but I\'m actually a noob on Java 8 features and don\'t know how to accomplish this. I have this simple line that contains the following text:

2条回答
  •  無奈伤痛
    2020-12-08 02:07

    String[]::new is a function that invokes the new "pseudo-method" for the String[] type just like String::trim is a function that invokes the real trim method of the String type. The value passed to the String::new function by toArray is the size of the collection on the right-hand side of the .toArray() method invocation.

    If you replaced String[]::new with n->new String[n] you might be more comfortable with the syntax just like you could replace String::trim with the less cool s->s.trim()

提交回复
热议问题