Monday, April 28, 2008

Collection and Set interface

Its easy to note that the collection interface and set interface both have same methods... but the Set interface doesnt allow duplicates and the Collection interface does allow.. why is it so ??

Reason ...
Every Set IS A Collection. The reason to extend Collection is that the contract is different. Collection says nothing about duplicates. Set is not allowed to contain duplicates. Even though there are no additional methods in Set, as a developer, we know that a Set will have this additional characteristic. If there is a parameter declared as a Set, then any developer using it knows that it will not contain duplicates, and any developer assigning to it knows that he can't use a collection that allows duplicates.