Does collection <E> and set <E> match?
Set compared to the concept of a Collection is to actually formally distinguish between the concepts. Let's say you write a method void x(Collection<?> c); You will not have the same idea about what arguments you want to receive, as if you wrote
void x(Set<?> s); The second method expects Collections , which contains each item no more than once (i.e. Sets ). This is a big semantic difference with the first method, which does not care if it gets Sets , Lists or any other type of Collection
If you look closely, the Set method Set also different, clearly demonstrating the various concepts that come into play when it comes to Collection or Set
From the Collection documentation:
A collection is a group of objects known as its elements. Some collections allow you to duplicate items, while others do not. Some of them are ordered, while others are disordered.
From the documentation of Set :
A collection that does not contain duplicate elements. More formally, sets do not have a pair of elements
e1ande2, such ase1.equals(e2)and no more than onenullelement. As its name implies, this interface models a mathematical abstract abstraction.
This should clarify the difference between a Set and a (more general interface) Collection .
Everything in the documentation:
Set is a collection that does not contain duplicate elements. More formally, the sets do not contain pairs of elements e1 and e2 such that e1.equations (e2) and at are the very same zero element. As can be seen from his name, this interface models a mathematical setting of abstraction.
and
Collection. The root interface in the collection hierarchy. A collection is a group of objects known as its elements. Some collections allow you to duplicate items and others do not. Some are ordered and others are unordered. The SDK does not provide any direct implementations of this interface: it provides the implementation of more specific subinterfaces, such as Set and List. This interface is commonly used to transfer collections around and manipulate them where maximum generality is required.
A distinction should be made between future implementation and use.
It came from set theory and vocabulary
A collection is something that is being assembled; a group of objects or the amount of material accumulated in one place, especially for any purpose or as a result of some process
In addition, the Set documentation defines a contract for .equals , which states that "only other sets can be equal to this set." If we could not recognize other sets by their type (using instanceof ), it would be impossible to implement.
If this were only for equals() , one could have the allowsDuplicates() method for Collection . But there are often times when the APIs want to say “please don’t give me duplicates” or “I guarantee that it does not contain duplicates”, and in Java there is no way to say in the method declaration “please give only collections, allowsDuplicates() returns false ". Thus, an additional type.