API Java 5的多:我应该返回一个数组或集合? [英] API java 5 and more: should I return an array or a Collection?

查看:169
本文介绍了API Java 5的多:我应该返回一个数组或集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最佳实践的精神:总是返回一个____,从来没有一个____ ,我从JDK1.4.2到JDK5和我即将移民面临着类似的问题更多信息。 (是的,我的知道的, JDK1.4.2是EOL !; - 。))

有关函数返回一个集合(这不是简单的财产集合) ,我总是preFER(在JDK1.4.2),而不是返回一个泛型列表的阵列,这是因为:


  • 它强制返回类型(为MyObject [],而不是对象列表,更多的类型安全在一个静态的 - 在编辑 - 水平)

  • 它的建议的一个'只读'字,以返回的集合(这是比较复杂的一个元素添加到集合中,尽管这不是为只读的严谨在C#关键字)。这不等于说是'不变',因为数组中的参考文献仍然可以修改...

当然,我也总是创建的这个返回数组(我不暴露任何内部数组)

现在,在JDK5多,我可以使用List<&MyObject的GT;如果我想。

在Java5中的编码时,为MyObject>

是什么充分的理由选择返回MyObject来的[]不是List或收集和LT?

奖金,如果收集和LT;用于为MyObject>,是有可能:


  • 执行只读在返回的集合属性? (无添加()或remove()可能)

  • 执行一个不变的方面返回集合? (该集合连引用不能被修改)

PS:本 JavaGenericFAQ 不太有一个


解决方案

preFER集合(或列表,或适当设置)到一个数组。有了泛型你仍然缺乏pre-Java 5的同时,通过只露出接口类型检查,你可以自由以后改变实现(例如切换为LinkedList的一个ArrayList)。

数组和仿制药不要混合得非常好。所以,如果你想利用仿制药,你应该的一般的避免阵列。

即:不能笼统创建一个数组。例如,如果T是一个泛型类型,然后新的T [0]不能编译。你必须做这样的事情(T [])新对象[0]​​,这会产生一个未经检查的投警告。出于同样的原因,你不能使用泛型类型可变参数无警告。

使用<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/Collections.html#unmodifiableCollection(java.util.Collection)\">Collections.unmodifiableCollection (以及类似的方法),你得到的只读约束(你不能用数组实现 - 你将不得不返回数组的克隆)。

您不能强制成员国不可改变,但你不能做到这一点与数组无论是。

In the spirit of Best Practices: Always return a ____, never a ____, I face a similar question in my upcoming migration from JDK1.4.2 to JDK5 and more. (Yes, I know, JDK1.4.2 is EOL! ;-) ).

For functions returning a collection (which are not simple property collections), I always prefer (in JDK1.4.2) returning an Array instead of a generic List, because:

  • it enforces the returning type (MyObject[] instead of List of Objects, much more type-safe on a static -- as in 'compilation' -- level)
  • it suggests a 'read-only' character to the returned collection (it is more complicated to add an element to the collection, even though this is not as rigorous as the 'read-only' keyword in c#). This is not the same as saying it is 'immutable' since the references inside the array can still be modified...

Of course, I do always create this returned array (I do not expose any 'internal' array)

Now, In JDK5 and more, I could use List<MyObject> if I want to.

What are the good reasons for choosing to return MyObject[] instead of List or Collection<MyObject> when coding in java5 ?

Bonus, if Collection<MyObject> is used, is it possible to:

  • enforce a read-only attribute on the returned collection ? (no add() or remove() possible)
  • enforce an immutable aspect to the returned collection ? (even the references of that collection can not be modified)

PS: The JavaGenericFAQ did not quite have that one.

解决方案

Prefer Collection (or List, or Set as appropriate) to an array. With generics you get the type-checking that was lacking pre-Java 5. Also, by exposing only the interface, you are free to change the implementation later (e.g. switch an ArrayList for a LinkedList).

Arrays and generics don't mix very well. So, if you want to take advantage of generics, you should usually avoid arrays.
I.e: You can't generically create an array. For example, if T is a generic type then "new T[0]" doesn't compile. You'd have to do something like "(T[]) new Object[0]", which generates an unchecked cast warning. For the same reason, you can't use generic types with varargs without warnings.

Using Collections.unmodifiableCollection (and similar methods), you get the read-only constraint (which you can't achieve with an array - you would have to return a clone of the array).

You can't enforce immutability of members, but then you can't do that with an array either.

这篇关于API Java 5的多:我应该返回一个数组或集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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