区别这两种方法转换集合数组对象 [英] Difference in these two approach for converting collection to array object

查看:115
本文介绍了区别这两种方法转换集合数组对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是下转换集合数组对象的方法在这两个根本区别

what is fundamental difference in these two following approach for converting collection to array object

 ArrayList<String> iName = new ArrayList<String>();

 String[] array= iName.toArray(new String[iName.size()]);//1
 String[] array= iName.toArray(new String[0]);//2

在应该使用方法1和方法时-2?

when should use approach-1 and when approach-2?

推荐答案

这两种方法之间的区别:

The difference between these two approaches:

    String[] array = iName.toArray(new String[iName.size()]);
    String[] array = iName.toArray(new String[0]);

是完全不同的,如果有一种可能性,即源集合( INAME 在这种情况下)可以同时修改。如果没有并发的可能性,那么,第一行是preferable

is quite different if there is a possibility that the source collection (iName in this case) can be modified concurrently. If there is no possibility of concurrency, then the first line is preferable.

这并不严格适用于你的榜样,其中源收集的ArrayList ,这是不是线程安全的。但是如果源集合是并发收集,然后并发修改下的行为是显著。在这种情况下,第一行具有竞争状态。调用线程首先获取大小;然后另外一个线程可能会添加或删除元素,改变它的大小。但随后调用线程分配与的大小数组并将其传递给的toArray

This doesn't strictly apply in your example, where the source collection is ArrayList, which is not thread-safe. But if the source collection is a concurrent collection, then the behavior under concurrent modification is significant. In this case, the first line has a race condition. The calling thread first gets the size; another thread might then add or remove an element, changing its size. But then the calling thread allocates the array with the old size and passes it to toArray.

这不会导致错误。如果你读的 Col​​lection.toArray(T []) 谨慎。如果集合长大,一个新的阵列将被分配给它,使主叫方的数组分配多余的。如果集合已经萎缩,数组的尾部最终会与null元素。这不一定是一个错误,但任何code消耗所产生的阵列是ppared处理空值$ P $,这可能是不愉快的。

This doesn't result in error. The cases are well-defined if you read the specification of Collection.toArray(T[]) carefully. If the collection had grown, a new array would be allocated for it, making the caller's array allocation redundant. If the collection had shrunk, the tail of the array would end up with null elements. This isn't necessarily an error, but any code that consumes the resulting array has to be prepared to handle nulls, which may be unpleasant.

传递一个零长度数组避免了这些问题,在分配一个无用的零长度阵列的成本。 (有人可能会保持一个缓存版本左右,但它增加了混乱。)如果集合是空的,只是返回零长度的数组。如果集合具有元件,虽然,集合本身分配正确大小的阵列和填充。这就避免了错误大小的数组主叫方移交的竞争条件。

Passing a zero-length array avoids these issues, at the possible cost of allocating a useless zero-length array. (One could keep a cached version around, but it adds clutter.) If the collection is empty, the zero-length array is simply returned. If the collection has elements, though, the collection itself allocates the array of the correct size and fills it. This avoids the race condition of the caller handing in an array of the "wrong" size.

在Java 8流API,请查看 Stream.toArray 方法通过使主叫方传递一个数组的工厂避免了这个问题。这使主叫方指定类型,但允许集合指定适当的大小。这还没有作出改进,以收藏虽然。这是由RFE JDK-8060192 。

In the Java 8 Streams API, the Stream.toArray method avoids this issue by having the caller pass in an array factory. This lets the caller specify the type but allows the collection to specify the proper size. This hasn't yet been retrofitted to Collections though. This is covered by an RFE JDK-8060192.

这篇关于区别这两种方法转换集合数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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