什么是“String [] :: new”意思? [英] What does "String[]::new" mean?

查看:235
本文介绍了什么是“String [] :: new”意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用流,我遇到了这个方法的问题。

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**);
}

我对 String []感到困惑: :new ,你能给我一个提示吗?

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

推荐答案

String [ ] :: new 表示尺寸 - > new String [size]

Stream#toArray(IntFunction< A []> generator) 已准备好生成一个数组,它调用 generator 并传递( generator.apply )内部集合的大小,以获得一个集合来填补它。

When Stream#toArray(IntFunction<A[]> generator) is ready to produce an array, it calls generator and passes (generator.apply) the size of the inner collection to get a collection to fill it up.

这篇关于什么是“String [] :: new”意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆