当数组足够时使用 Java List

发布于 2024-10-17 05:42:06 字数 116 浏览 1 评论 0原文

当您事先知道列表的大小并且也可以在那里使用数组的情况下,是否建议使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

迷离° 2024-10-24 05:42:06

在您事先知道列表大小并且也可以在那里使用数组的情况下,是否建议使用 Java 集合列表?

在某些(可能是大多数)情况下,是的,无论如何,绝对建议使用集合,但在某些情况下,这是不可取的。

从优点来看:

  • 如果您使用列表而不是数组,您的代码可以使用 containsinsertremove 等方法。
  • 许多库类都需要集合类型的参数。
  • 您无需担心代码的下一个版本可能需要更动态调整大小的数组……这将使基于数组的初始方法成为一种负担。

缺点:

  • 集合有点慢,如果数组的基类型是原始类型,则更慢。
  • 集合确实需要更多内存,特别是当数组的基类型是原始类型时。

但性能很少是一个关键问题,并且在许多情况下,性能差异与整体情况无关。

在实践中,计算数组的大小通常会带来性能和/或代码复杂性方面的成本。 (考虑一下假设的情况,您使用 char[] 来保存一系列的串联。您可以算出数组需要多大;例如,通过将组件字符串的大小。但是很乱!)

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 ?

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:

  • If you use an List instead of an array, your code can use methods like contains, insert, remove and so on.
  • A lot of library classes expect collection-typed arguments.
  • You don't need to worry that the next version of the code may require a more dynamically sized array ... which would make an initial array-based approach a liability.

On the con side:

  • Collections are a bit slower, and more so if the base type of your array is a primitive type.
  • Collections do take more memory, especially if the base type of your array is a primitive type.

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!)

悲凉≈ 2024-10-24 05:42:06

集合/列表更加灵活,并提供更多实用方法。对于大多数情况,任何性能开销都是可以忽略不计的。

对于此单个语句初始化,请使用:

Arrays.asList(yourArray);

From 文档

返回由指定数组支持的固定大小列表。 (对返回列表的更改“写入”数组。)此方法与 Collection.toArray 结合使用,充当基于数组的 API 和基于集合的 API 之间的桥梁。返回的列表是可序列化的并实现 RandomAccess。

我的猜测是,这是转换为列表的最注重性能的方法,但我可能是错的。

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:

Arrays.asList(yourArray);

From the docs:

Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.) This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray. The returned list is serializable and implements RandomAccess.

My guess is that this is the most performance-wise way to convert to a list, but I may be wrong.

刘备忘录 2024-10-24 05:42:06

1) Collection 是最基本的类型,仅意味着存在对象的集合。如果没有顺序或重复则使用java.util.Set,如果有可能重复且排序则使用java.util.List,是否有顺序但没有重复使用java.util.SortedSet

2) 大括号实例化数组,Arrays.asList() 加上用于类型推断的泛型

List<String> myStrings = Arrays.asList(new String[]{"one", "two", "three"});

还有一个使用匿名类型的技巧,但我个人不太喜欢:

List<String> myStrings = new ArrayList<String>(){
// this is the inside of an anonymouse class
{
    // this is the inside of an instance block in the anonymous class
    this.add("one");
    this.add("two");
    this.add("three");
}};

1) a Collection is the most basic type and only implies there is a collection of objects. If there is no order or duplication use java.util.Set, if there is possible duplication and ordering use java.util.List, is there is ordering but no duplication use java.util.SortedSet

2) Curly brackets to instantiate an Array, Arrays.asList() plus generics for the type inference

List<String> myStrings = Arrays.asList(new String[]{"one", "two", "three"});

There is also a trick using anonymous types but personally I'm not a big fan:

List<String> myStrings = new ArrayList<String>(){
// this is the inside of an anonymouse class
{
    // this is the inside of an instance block in the anonymous class
    this.add("one");
    this.add("two");
    this.add("three");
}};
苍风燃霜 2024-10-24 05:42:06

是的,这是可取的。

一些不同的列表构造函数(如 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.

治碍 2024-10-24 05:42:06

有不同的事情需要考虑:数组的类型是否已知?谁访问数组?
数组有几个问题,例如:

  • 您无法创建通用数组
  • 数组是协变的:如果 A 扩展 B -> > A[] 扩展了 B[],这可能会导致 ArrayStoreExceptions
  • 您无法使数组的字段不可变
  • ...

另请参阅《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.:

  • you can not create generic arrays
  • arrays are covariant: if A extends B -> A[] extends B[], which can lead to ArrayStoreExceptions
  • you cannot make the fields of an array immutable
  • ...

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.

How can a list be initialised with elements in a single statement like an array = {list of all elements separated by commas} ?

Arrays.asList(): http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29

陈甜 2024-10-24 05:42:06

在您事先知道列表大小并且也可以在那里使用数组的情况下,是否建议使用 Java Collections List?性能缺点???

如果一个数组就足够了,那就用数组。只是为了让事情变得简单。您甚至可能会获得稍微更好的性能。请记住,如果您...

  • 需要将结果数组传递给采用 Collection 的方法,或者
  • 如果您需要使用 List 方法,例如如 .contains.lastIndexOf 等,或者
  • 如果您需要使用 Collections 方法,例如 reverse >...

那么也可以从头开始使用 Collection/List 类。

如何使用单个语句中的元素初始化列表,例如数组 = {用逗号分隔的所有元素的列表}?

您可以执行

List<String> list = Arrays.asList("foo", "bar");

List<String> arrayList = new ArrayList<String>(Arrays.asList("foo", "bar"));

List<String> list = new ArrayList<String>() {{ add("foo"); add("bar"); }};

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 ? Performance drawbacks ???

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...

  • ever need to pass the resulting array to a method that takes a Collection, or
  • if you ever need to work with List-methods such as .contains, .lastIndexOf, or what not, or
  • if you need to use Collections methods, such as reverse...

then may just as well go for the Collection/List classes from the beginning.

How can a list be initialised with elements in a single statement like an array = {list of all elements separated by commas} ?

You can do

List<String> list = Arrays.asList("foo", "bar");

or

List<String> arrayList = new ArrayList<String>(Arrays.asList("foo", "bar"));

or

List<String> list = new ArrayList<String>() {{ add("foo"); add("bar"); }};
宣告ˉ结束 2024-10-24 05:42:06

是否建议使用Java
收藏清单在以下情况下,当您
事先知道列表的大小
你也可以在那里使用数组?
性能缺点?

即使您事先知道大小,使用列表而不是数组也是完全可以接受的。

如何初始化列表
单个语句中的元素,例如
array = {所有元素的列表
用逗号分隔} ?

请参阅Arrays.asList()。

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 ?
Performance drawbacks ?

It can be perfectly acceptable to use a List instead of an array, even if you know the size before hand.

How can a list be initialised with
elements in a single statement like an
array = {list of all elements
separated by commas} ?

See Arrays.asList().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文