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 something here ?
6 Answers
Set doesn't allow duplicates.
It's a semantic difference, not a syntactic one.
3 Comments
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
e1ande2such thate1.equals(e2), and at most onenullelement. 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.
Comments
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
4 Comments
Set parameter in my method, and not just a plain Collection. That's one of many reasonsjava.util.Map<K, V> interface. It has these two methods, for instance: Set<K> keySet() and Collection<V> values(). It is known that every key can only be contained once in the Map and the ordering of keys is irrelevant. So a Set is a better choice than a Collection because it formally communicates that fact. For values() it's different. We don't know whether a value can be contained several times in a map and the ordering is irrelevant too. So the best choice is a CollectionCollection is a more generic interface which comprises of Lists, Queues, Sets and many more.
Have a look at the 'All Known Subinterfaces' section here.
Comments
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
Comments
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.