开发者

Collection<E> and Set<E> are the same?

I have a question about those two interfaces in Java. Set extends Collection, but doesn't add anything. They are exactly the same. Am I missing somethi开发者_运维问答ng here ?


Set doesn't allow duplicates.

It's a semantic difference, not a syntactic one.


From the documentation of Collection:

A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.

From the documentation of Set:

A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

That should clarify the difference between a Set and a (the more general interface) Collection.


Good question. I guess the main purpose of explicitly having an interface for the concept of a Set as compared to the concept of a Collection is to actually formally distinguish the concepts. Let's say you're writing a method

void x(Collection<?> c);

You won't have the same idea of what arguments you want to get, as if you were writing

void x(Set<?> s);

The second method expects Collections that contain every element at most once (i.e. Sets). That's a big semantic difference to the first method, which doesn't care whether it receives Sets, Lists or any other type of Collection

If you look closely, the Javadoc of the Set method is different as well, explicitly showing the different notions that come into play when talking about Collection or Set


Collection is a more generic interface which comprises of Lists, Queues, Sets and many more.

Have a look at the 'All Known Subinterfaces' section here.


Everything is in the documentation:

Set - A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction.

and

Collection - The root interface in the collection hierarchy. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered. The SDK does not provide any direct implementations of this interface: it provides implementations of more specific subinterfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.

It is only to distinguish the implementation and future usage.

This came from the Set theory and dictionary

Collection - something that is collected; a group of objects or an amount of material accumulated in one location, especially for some purpose or as a result of some process

Set - is a collection of distinct objects


Additionally, the Set documentation defines a contract for .equals, which says "only other Sets may be equal to this Set". If we couldn't recognize the other Sets by their type (with instanceof), it would be impossible to implement this.

If it were only for equals(), it would be possible to have a allowsDuplicates() method for Collection. But there are often cases where APIs want to say "please don't give me duplicates" or "I guarantee that this does not contain duplicates", and in Java there is no way to say in a method declaration "please give only collections whose allowsDuplicates() method returns false". Thus the additional type.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜