当数组足够时使用 Java List
当您事先知道列表的大小并且也可以在那里使用数组的情况下,是否建议使用Java Collections List?有性能缺陷吗?
可以像数组一样用单个语句中的元素初始化列表(所有元素用逗号分隔的列表)吗?
Is it advisable to use Java Collections List in the cases when you know the size of the list before hand and you can also use array there? Are there any performance drawbacks?
Can a list be initialised with elements in a single statement like an array (list of all elements separated by commas) ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在某些(可能是大多数)情况下,是的,无论如何,绝对建议使用集合,但在某些情况下,这是不可取的。
从优点来看:
contains
、insert
、remove
等方法。缺点:
但性能很少是一个关键问题,并且在许多情况下,性能差异与整体情况无关。
在实践中,计算数组的大小通常会带来性能和/或代码复杂性方面的成本。 (考虑一下假设的情况,您使用
char[]
来保存一系列的串联。您可以算出数组需要多大;例如,通过将组件字符串的大小。但是很乱!)In some (probably most) circumstances yes, it is definitely advisable to use collections anyway, in some circumstances it is not advisable.
On the pro side:
contains
,insert
,remove
and so on.On the con side:
But performance is rarely a critical issue, and in many cases the performance difference is not relevant to the big picture.
And in practice, there is often a cost in performance and/or code complexity involved in working out what the array's size should be. (Consider the hypothetical case where you used a
char[]
to hold the concatenation of a series. You can work out how big the array needs to be; e.g. by adding up the component string sizes. But it is messy!)集合/列表更加灵活,并提供更多实用方法。对于大多数情况,任何性能开销都是可以忽略不计的。
对于此单个语句初始化,请使用:
From 文档:
我的猜测是,这是转换为列表的最注重性能的方法,但我可能是错的。
Collections/lists are more flexible and provide more utility methods. For most situations, any performance overhead is negligible.
And for this single statement initialization, use:
From the docs:
My guess is that this is the most performance-wise way to convert to a list, but I may be wrong.
1)
Collection
是最基本的类型,仅意味着存在对象的集合。如果没有顺序或重复则使用java.util.Set
,如果有可能重复且排序则使用java.util.List
,是否有顺序但没有重复使用java.util.SortedSet
2) 大括号实例化数组,Arrays.asList() 加上用于类型推断的泛型
还有一个使用匿名类型的技巧,但我个人不太喜欢:
1) a
Collection
is the most basic type and only implies there is a collection of objects. If there is no order or duplication usejava.util.Set
, if there is possible duplication and ordering usejava.util.List
, is there is ordering but no duplication usejava.util.SortedSet
2) Curly brackets to instantiate an Array, Arrays.asList() plus generics for the type inference
There is also a trick using anonymous types but personally I'm not a big fan:
是的,这是可取的。
一些不同的列表构造函数(如 ArrayList)甚至可以接受参数,以便您可以“预分配”足够的后备存储,从而减轻在添加元素时列表“增长”到适当大小的需要。
Yes, it is advisable.
Some of the various list constructors (like ArrayList) even take arguments so you can "pre-allocate" sufficient backing storage, alleviating the need for the list to "grow" to the proper size as you add elements.
有不同的事情需要考虑:数组的类型是否已知?谁访问数组?
数组有几个问题,例如:
另请参阅《Effective Java》一书的第 25 项“优先使用列表而不是数组”。
也就是说,有时数组很方便,例如新的 Object... 参数语法。
Arrays.asList(): http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29
There are different things to consider: Is the type of the array known? Who accesses the array?
There are several issues with arrays, e.g.:
Also see, item 25 "Prefer lists to arrays" of the Effective Java book.
That said, sometimes arrays are convenient, e.g. the new Object... parameter syntax.
Arrays.asList(): http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29
如果一个数组就足够了,那就用数组。只是为了让事情变得简单。您甚至可能会获得稍微更好的性能。请记住,如果您...
Collection
的方法,或者List
方法,例如如.contains
、.lastIndexOf
等,或者Collections
方法,例如reverse
>...那么也可以从头开始使用
Collection
/List
类。您可以执行
或
或
If an array is enough, then use an array. Just to keep things simple. You may even get a slightly better performance out of it. Keep in mind that if you...
Collection
, orList
-methods such as.contains
,.lastIndexOf
, or what not, orCollections
methods, such asreverse
...then may just as well go for the
Collection
/List
classes from the beginning.You can do
or
or
即使您事先知道大小,使用列表而不是数组也是完全可以接受的。
请参阅Arrays.asList()。
It can be perfectly acceptable to use a List instead of an array, even if you know the size before hand.
See
Arrays.asList()
.