java中的String[]::new和new String[]有区别吗? [英] Is there a difference between String[]::new and new String[] in java?

查看:100
本文介绍了java中的String[]::new和new String[]有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

ArrayList<String> var1 = new ArrayList<>();
for(condition){
// Add items to var1
}

return var1.toArray(new String[]{})

我的 IDE 建议我将最后一行更改为

My IDE is recommending i change the last line to

return var1.toArray(String[]::new)

这是更好的约定还是编译时或内存使用也有一些好处.

Is this just better convention or is there also some benefits to compile-time or memory usage.

推荐答案

String[]::new 是一个函数.它不创建新的字符串数组,它定义了一个创建新字符串数组的函数.这就像蛋糕和制作蛋糕的食谱之间的区别.一种由面粉和糖衣组成.另一种只是文字和纸张.完全不同的东西,但相关的,因为食谱可以让你做蛋糕(任意数量,甚至!)

String[]::new is a function. It does not create a new string array, it defines a function that makes new string arrays. It's like the difference between a cake, and a recipe to make a cake. One consists of flour and icing and all that. The other one is just words and paper. Completely different things, but related, in that the recipe lets you make cake (any number of them, even!)

String[]::new 是以下的简写*:

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

仅仅编写根本不会产生任何字符串数组;调用该函数将在每次调用时生成一个字符串数组.

Merely writing that doesn't make any string arrays at all; calling that function would make one string array each time it is invoked.

示例:

IntFunction<String[]> stringArrayMaker = String[]::new;
String[] size10 = stringArrayMaker.apply(10);

好的,那么推荐呢?

你写的有点不标准;new String[0] 更标准.所以让我们比较 new String[0]String[]::new.

Okay, so what about the recommendation?

What you've written is slightly non-standard; new String[0] is more standard. So let's compare new String[0] versus String[]::new.

这个参数唯一的一点就是让arraylist知道你想要什么类型.您可能认为 arraylist 已经知道(毕竟它是一个 ArrayList!)但是由于泛型擦除(一个相当复杂的主题), 的代码没有办法toArray 知道你想要什么,因此,你必须指定.

The only point of this parameter is to allow the arraylist to know what type you want. You'd think the arraylist already knows (it is an ArrayList<String> after all!) but due to generics erasure (a rather complex topic), there is no way for the code of toArray to know what you want, therefore, you must specify.

new String[0] 最终生成一个长度为 0 的字符串数组,然后立即丢弃该数组.这感觉很浪费,但垃圾收集就是这样,快速垃圾"(创建和丢弃的对象非常快,从不与其他线程共享)几乎完全免费.此方法 (toArray(T[])) 的工作原理如下:如果传入的数组与所需的数组一样大或更大,则将其填充并返回.如果它太小,则创建一个具有相同组件类型且大小合适的新数组,填充一个,然后返回一个.

new String[0] ends up making a 0-length string array which is then immediately discarded. This feels wasteful, but garbage collection being what it is, 'quick garbage' (objects made and discarded very quickly and never shared with other threads) is almost entirely free. This method (toArray(T[])) works as follows: If the incoming array is as large or larger than is needed, it is filled, and returned. If it is too small, a new array is created with the same component type of the right size, that one is filled, and that one is returned.

因此,您会认为 var1.toArray(new String[var1.size()]) 是最有效的.但是 JMH 告诉我根本没有区别.这表明 Java 在处理短期垃圾方面的效率是多么令人难以置信(当我说短期垃圾是免费的"时,我是认真的.几乎不可能见证它的成本).

You'd think, therefore, that var1.toArray(new String[var1.size()]) is the most efficient. But JMH tells me there is no difference at all. Which goes to show how incredibly efficient java is at dealing with short lived garbage (when I say 'short lived garbage is free', I mean it. It's almost impossible to witness the cost of it).

函数式方法(你传递 String[]::new 的地方)意味着这个函数将负责制作数组,而 toArray 本身只依赖于什么它得到.这..也很好用.它最终会在您的类文件中的某个地方创建一个方法.

The functional approach (where you pass String[]::new) means that this function will take care of making the array, and toArray itself just relies on what it gets. This.. also works fine. It ends up making a method in your class file someplace.

所以,这真的无关紧要.我发现 IDE 在这里过于说教了.String[]::new 创建了一个新方法,单是类空间的开销可能意味着它最终的效率较低,即使垃圾的创建更容易理解效率低下.

So, it really doesn't matter. I find the IDE being overly preachy here. String[]::new makes a new method, and the overhead in class space alone probably means it ends up being less efficient, even though the creation of the garbage is more easily understandable inefficiency.

但是,我们在这里真的很挑剔.字面意思是说纳秒 - 您永远不会注意到.

But, we're really nitpicking here. Literally talking about nanoseconds - you're never going to notice.

做任何你认为最易读的事情(尽管我会避开 new String[]{} - 它本身并没有什么问题,但它不是很常见,它没有比 new String[0] - 如果不重要的话,也可以做社区做的事情).

Do whatever you find is most readable (though I would steer clear of new String[]{} - nothing wrong with it perse, but it's not very common and it has no benefits over new String[0] - might as well do what the community does if it doesn't otherwise matter).

*) 这有点过于简单化了,但不是太多.

*) This is slightly oversimplified, but not by much.

这篇关于java中的String[]::new和new String[]有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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