集合vs列表vs数组作为EJB方法的返回类型 [英] collection vs list vs arrays as return type for EJB method

查看:191
本文介绍了集合vs列表vs数组作为EJB方法的返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近被告知,集合应该优先于List作为EJB方法的返回值。参数是,一般来说,集合是更通用的,即让您更改底层数据结构,而不影响客户端。如果这是你想要作为设计师的灵活性,那么使用集合将更好的意义。但是,它不会更有意义只返回一个数组而不是集合?

I was recently told that collection should be preferred to List as the return value of an EJB method. The argument is that in general collection is more generic i.e. gives you to change the underlying data structure without impacting the clients. And if this is the flexibility you want to have as a designer then using collection will make better sense. But then wouldn't it make more sense to return just an array instead of collection?

如果有任何影响,对性能有何影响?

And what are performance impacts if any?

提前感谢。

推荐答案


  • 首选通过数组的集合;使用泛型

  • 使用接口而非具体类

  • code>列表,设置 Iterable

    Then you usually have 4 options: List, Set, Collection and Iterable. There it depends what is the semantics you want to include.


    • 如果是内部API - 根据集合的特性决定它:

      • 它只保留唯一项目吗? 设置

      • 客户端需要随机访问吗? 列表

      • 客户需要修改它(添加,删除) 集合

      • 客户端只需要迭代它? Iterable

      • If it is an internal API - decide it based on the characteristics of the collection:
        • does it only hold unique items? Set
        • will clients need random access to it? List
        • will clients need to modify it (add, remove) (without the above two characteristics)? Collection
        • will clients just need to iterate it? Iterable

        (注意:有一些具有更多特定语义的集合接口: Queue Multiset 等 - 但是当你需要返回它们时会很明显)

        (Note: there are some collection interfaces with more specific semantics: Queue, Deque, Map, Bag, Multiset, etc. - but it will be fairly obvious when you need to return them)

        这篇关于集合vs列表vs数组作为EJB方法的返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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