简体   繁体   English

转换由 List.of 或 ArrayList 设置?

[英]convert Set by List.of or ArrayList?

I want to convert a set to a list.我想将一组转换为列表。 Which method should I prefer (I dont care if it's immutable or not):我应该更喜欢哪种方法(我不在乎它是否不可变):

version 1:版本 1:

return List.of(myHashSet().toArray());

or version 2:或版本 2:

return new ArrayList<Strategy>(myHashSet());

Are there any other differences besides that version 1 is immutable?除了版本 1 是不可变的之外,还有其他区别吗? For example, List.of is more space efficient in compare to ArrayList.例如,与List.of相比,List.of 的空间效率更高。

The only differences I see between your 1 and 2 are:我在您的 1 和 2 之间看到的唯一区别是:

  • List.of produces a shallowly immutable list. List.of产生一个浅不可变的列表。 You cannot add or remove items from the list.您不能在列表中添加或删除项目。 Of course, the state within each object contained in the list would still be mutable if already so.当然,如果列表中包含的每个 object 中的 state 已经是可变的,那么它仍然是可变的。 Your # 1 results in an immutable list while the ArrayList of # 2 is mutable.你的#1 导致一个不可变的列表,而#2 的ArrayList是可变的。
  • With # 2, you know and control the concrete class of the resulting List .使用#2,您知道并控制生成的List的具体 class 。 With # 1, you neither know nor control the concrete class used behind the scenes when calling List.of .使用#1,您既不知道也不控制调用List.of The List.of feature is free to use any class that implements List interface, possibly even a class not publicly available with the Java Collections Framework. List.of功能可免费使用任何实现List接口的 class,甚至可能是 class 未随 Java class 公开可用的 class The List.of feature might even be smart about choosing an optimized class appropriate to your particular collected objects. List.of功能甚至可以聪明地选择适合您特定收集对象的优化 class。

You said:你说:

List.of is more space efficient in compare to ArrayList.与 ArrayList 相比,List.of 的空间效率更高。

You cannot make that claim, if you mean usage of memory (RAM).如果您的意思是使用 memory (RAM),您不能提出该声明。 As discussed above, you neither know nor control the choice of class used to implement List interface when calling List.of .如上所述,在调用List.of时,您既不知道也不控制用于实现List接口的 class 的选择。 The class used might vary depending on your data, and might vary by version of Java used at runtime.使用的 class 可能因您的数据而异,并且可能因运行时使用的 Java 版本而异。

As for memory used by the call to toArray , an array of objects is really just an array of references (pointers).至于toArray调用使用的 memory ,对象数组实际上只是引用(指针)数组。 Creating that array takes little memory and is fast.创建该数组需要很少的 memory 并且速度很快。 Object references are likely to be a value of four or eight octets (depending on your JVM being 32-bit or 64-bit), though not specified by Java. Object 引用可能是四个或八个八位字节的值(取决于您的JVM是 32 位还是 64 位),尽管 ZD52387880E1EA22817A72D7.1 没有指定。 In creating the array, it is not as if the content of your element objects are being duplicated.在创建数组时,元素对象的内容并没有被复制。 So for most common apps the brief creation and disposal of that array would be insignificant.因此,对于大多数常见应用程序而言,该数组的简短创建和处置将是微不足道的。

And, as commented by Kuhn , Java 10 saw the arrival of a new factory method List.copyOf that takes a Collection .而且,正如Kuhn 所评论的,Java 10 看到了一个新的工厂方法List.copyOf的到来,它需要一个Collection So no need to call toArray .所以不需要调用toArray

List<Strategy> myStrategyList = List.copyOf( myStrategySet ) ;

Conclusion:结论:

  • If you need an immutable list, use List.copyOf .如果您需要不可变列表,请使用List.copyOf
  • If your need mutability or any other feature specific to a particular implementation of List , use that particular class.如果您需要可变性或特定于List特定实现的任何其他功能,请使用该特定 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM