4

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 6

8

Set doesn't allow duplicates.

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

Sign up to request clarification or add additional context in comments.

3 Comments

+1: specification is as important for interfaces as method definitions are.
So I don't get It. why is Set<E> is needed. It feels wrong OO-wise.
Insert two times the same object in a Set and in a Collection.
6

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.

Comments

5

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

Still, I don't understand. If Set<E> is only to imply by it's name that we have a collection with no multiple objects, and doesn't enforce it by any code change, why do we need it. If i am implementing collection, I can just call my class with a name that will imply that behavior.
Exactly for the reasons I mentioned (and the others here). Because then, I can formally expect a Set parameter in my method, and not just a plain Collection. That's one of many reasons
Im followng this thread.Would be appreciated if you could cite a working example which could be handy for further reference.
Hmm, cite an example... OK, go check out the java.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 Collection
2

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.

Comments

0

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.