Java 何时使用 Set 与 Collection?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
when to use Set vs. Collection?
提问by Jason S
Is there any practical difference between a Set
and Collection
in Java, besides the fact that a Collection
can include the same element twice? They have the same methods.
除了 a可以包含两次相同的元素这一事实之外,aSet
和Collection
Java 中的a和在 Java 中是否有任何实际区别Collection
?他们有相同的方法。
(For example, does Set
give me more options to use libraries which accept Set
s but not Collection
s?)
(例如,是否Set
给了我更多选项来使用接受Set
s 但不接受s 的库Collection
?)
edit:I can think of at least 5 different situations to judge this question. Can anyone else come up with more? I want to make sure I understand the subtleties here.
编辑:我可以想到至少 5 种不同的情况来判断这个问题。其他人能想出更多吗?我想确保我理解这里的微妙之处。
- designing a method which accepts an argument of
Set
orCollection
.Collection
is more general and accepts more possibilities of input. (if I'm designing a specific class or interface, I'm being nicer to my consumers and stricter on my subclassers/implementers if I useCollection
.) - designing a method which returns a
Set
orCollection
.Set
offers more guarantees thanCollection
(even if it's just the guarantee not to include one element twice). (if I'm designing a specific class or interface, I'm being nicer to my consumers and stricter on my subclassers/implementers if I useSet
.) - designing a class that implements the interface
Set
orCollection
. Similar issues as #2. Users of my class/interface get more guarantees, subclassers/implementers have more responsibility. - designing an interface that extends the interface
Set
orCollection
. Very similar to #3. - writing code that uses a
Set
orCollection
. Here I might as well useSet
; the only reasons for me to useCollection
is if I get back aCollection
from someone else's code, or if I have to handle a collection that contains duplicates.
- 设计一个接受
Set
or参数的方法Collection
。Collection
更通用,接受更多的输入可能性。(如果我正在设计一个特定的类或接口,如果我使用 ,我会对我的消费者更好并且对我的子类/实现者更严格Collection
。) - 设计一个返回 a
Set
或的方法Collection
。Set
提供比Collection
(即使只是保证不包含一个元素两次)更多的保证。(如果我正在设计一个特定的类或接口,如果我使用 ,我会对我的消费者更好并且对我的子类/实现者更严格Set
。) - 设计一个实现接口
Set
或的类Collection
。与#2 类似的问题。我的类/接口的用户得到更多的保证,子类/实现者有更多的责任。 - 设计一个扩展接口的接口
Set
或Collection
. 非常类似于#3。 - 编写使用
Set
或的代码Collection
。在这里我不妨使用Set
; 我使用的唯一原因Collection
是,如果我Collection
从其他人的代码中取回 a ,或者我必须处理包含重复项的集合。
采纳答案by Michael Myers
Collection
is also the supertype of List
, Queue
, Deque
, and others, so it gives you more options. For example, I try to use Collection
as a parameter to library methods that shouldn't explicitly depend on a certain type of collection.
Collection
也是超类型List
,Queue
,Deque
,和其他人,所以它给你更多的选择。例如,我尝试将Collection
不应显式依赖于某种类型集合的库方法用作参数。
Generally, you should use the right tool for the job. If you don't want duplicates, use Set
(or SortedSet
if you want ordering, or LinkedHashSet
if you want to maintain insertion order). If you want to allow duplicates, use List
, and so on.
通常,您应该使用正确的工具来完成这项工作。如果您不想重复,请使用Set
(或者SortedSet
如果您想要排序,或者LinkedHashSet
如果您想要维护插入顺序)。如果要允许重复,请使用List
,等等。
回答by Zack Marrapese
See Java's Collection tutorialfor a good walk-through of Collection usage. In particular, check out the class hierarchy.
请参阅 Java 的Collection 教程,以获得有关 Collection 用法的良好演练。特别是,检查类层次结构。
回答by George Armhold
I think you already have it figured out- use a Set
when you want to specifically exclude duplicates. Collection
is generally the lowest common denominator, and it's useful to specify APIs that accept/return this, which leaves you room to change details later on if needed. However if the details of your application require unique entries, use Set
to enforce this.
我认为您已经弄清楚了-Set
当您想专门排除重复项时使用 a 。 Collection
通常是最小的公分母,指定接受/返回它的 API 很有用,这样您就可以在以后根据需要更改详细信息。但是,如果您的应用程序的详细信息需要唯一的条目,请使用Set
来强制执行此操作。
Also worth considering is whether order is important to you; if it is, use List
, or LinkedHashSet
if you care about order anduniqueness.
同样值得考虑的是订单对你来说是否重要;如果是,请使用List
,或者LinkedHashSet
如果您关心顺序和唯一性。
回答by Tom
You should use a Set when that is what you want.
当您需要时,您应该使用 Set。
For example, a List without any order or duplicates. Methods like contains are quite useful.
例如,一个没有任何顺序或重复的列表。像 contains 这样的方法非常有用。
A collection is much more generic. I believe that what mmyers wrote on their usage says it all.
集合更通用。我相信 mmyers 写的关于他们的用法说明了一切。
回答by pkaeding
As @mmyers states, Collection includes Set, as well as List.
正如@mmyers 所说,Collection 包括 Set 和 List。
When you declare something as a Set, rather than a Collection, you are saying that the variable cannot be a List or a Map. It will always be a Collection, though. So, any function that accepts a Collection will accept a Set, but a function that accepts a Set cannot take a Collection (unless you cast it to a Set).
当您将某些内容声明为 Set 而不是 Collection 时,您是在说变量不能是 List 或 Map。不过,它永远是一个集合。因此,任何接受集合的函数都将接受集合,但接受集合的函数不能接受集合(除非您将其强制转换为集合)。
回答by Vincent Ramdhanie
The practical difference is that Set enforces the setlogic, i.e. no duplicates and unordered, while Collection does not. So if you need a Collection and you have no particular requirement for avoiding duplicates then use a Collection. If you have the requirement for Set then use Set. Generally use the highest interface possibble.
实际的区别是 Set 强制执行集合逻辑,即没有重复和无序,而 Collection 没有。因此,如果您需要一个集合并且您对避免重复没有特别的要求,那么请使用一个集合。如果您对 Set 有要求,请使用 Set。通常使用可能的最高接口。
回答by Clint Miller
One other thing to consider... Sets have extra overhead in time, memory, and coding in order to guarantee that there are no duplicates. (Time and memory because sets are usually backed by a HashMap or a Tree, which adds overhead over a list or an array. Coding because you have to implement the hashCode() and equals() methods.)
要考虑的另一件事...集合在时间、内存和编码方面有额外的开销,以确保没有重复项。(时间和内存,因为集合通常由 HashMap 或树支持,这会增加列表或数组的开销。编码,因为您必须实现 hashCode() 和 equals() 方法。)
I usually use sets when I need a fast implementation of contains() and use Collection or List otherwise, even if the collection shouldn't have duplicates.
我通常在需要快速实现 contains() 时使用集合,否则使用集合或列表,即使集合不应该有重复项。
回答by Peter Lawrey
As Collection is a super type of Set and SortedSet these can be passed to a method which expects a Collection. Collection just means it may or may not be sorted, order or allow duplicates.
由于 Collection 是 Set 和 SortedSet 的超类型,因此可以将它们传递给需要 Collection 的方法。集合只是意味着它可能会或可能不会被排序、排序或允许重复。