0

What's the purpose of java.util.Set interface if it contains absolutely equal methods in comparison with java.util.Collection? There is no prohibition for multi-implementing in Java, and Set realization (HashSet) could work fine without Set interfaces. So why do we need that useless baggage?

2
  • there's 7 other classes that implement the interface, not just hashset AbstractSet, ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, JobStateReasons, LinkedHashSet, TreeSet Commented Feb 6, 2013 at 20:32
  • You're right, but nevertheless - were those classes able to implement Collection interface and forget about Set or not? Commented Feb 6, 2013 at 20:33

2 Answers 2

1

JavaDoc describing contract of Collection (emphasis mine):

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

On Set:

A collection that contains no duplicate elements.

In other words, Set abstraction describes a (ekhem!) subset of Collections.

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

2 Comments

You mean that was made to protect methods that take (Set) as an argument from passing List's realizations ?
@user2033357: that means that if you implement Set interface you must obey the contract (thus, don't allow duplicates). If you implement Collection only (not Set) you are free to accept duplicates. It doesn't enforce anything, it basically documents what you can accept on every implementation. If some implementation of Set allows duplicates, it's broken by definition.
0

One difference is that Collection allows duplicate elements, and Set does not. There may be more.

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.