Set and Collection Interfaces

If we look at the Set and collection both are exactly the same... what could be the possible reasons why there are two different interfaces defined...
[157 byte] By [kayweea] at [2007-10-2 5:25:37]
# 1
A Set can't have duplicates. A Collection can.
MLRona at 2007-7-16 1:27:27 > top of Java-index,Core,Core APIs...
# 2
And, if you look at the hierarchy, all Sets are Collections, but not all Collections are Sets.
MLRona at 2007-7-16 1:27:27 > top of Java-index,Core,Core APIs...
# 3

> A Set can't have duplicates. A Collection can.

but how do we enforce that in interface definitions... having no duplicates can be implemented in the Abstract or Concrete classes which implement this interface. But we see two Intefaces in java.util package with same method signatures... so there should be some reason why they do it that way...

kayweea at 2007-7-16 1:27:27 > top of Java-index,Core,Core APIs...
# 4

> > A Set can't have duplicates. A Collection can.

>

> but how do we enforce that in interface

> definitions...

you don't! You implement an interface according to your needs. The interface provides a common "interface" between all classes that implement it. You inforce it in your implementation of the add()

method in the class that you are defining.

Arbiea at 2007-7-16 1:27:27 > top of Java-index,Core,Core APIs...
# 5

You can't enforce it in the interface definitions. But, if you write a method that can't allow duplicates in the Collection that it uses, you write the method to use a Set. If you can allow duplicates, you can just say Collection.

public Collection myCollectionMethod(String text)

{

Collection coll = new ArrayList()

coll.add(text);

coll.add(text);

return coll;

}

public Set mySetMethod(String text)

{

Set coll = new HashSet()

coll.add(text);

coll.add(text);

return coll;

}

Collection myColl = myCollectionMethod("Hello");

Set mySet = mySetMethod("Hello");

Collection mySecondSet = mySetMethod("Hello"); // okay--a Set is a Collection

Set mySecondCollection = myCollectionMethod("Hello"); // NOT OKAY--a Set is not necessarily a Collection

System.out.println(myColl.size()); // returns 2

System.out.println(mySet.size()); // returns 1

System.out.println(mySecondSet.size()); // returns 1

If you are told that you have a Set, you are guaranteed that you don't have duplicates. If you are told that you have a Collection, you have no such guarantee. It is up to the person who implements an interface to make the implementation do what the interface is intended to do. You could certainly make an 'add' method that removes an object instead of adding it, but the person who uses your method will get probably mad at you for doing so--he would be expecting the 'add' to do what it says, "add an object".

MLRona at 2007-7-16 1:27:27 > top of Java-index,Core,Core APIs...
# 6

> but how do we enforce that in interface

> definitions... having no duplicates can be

> implemented in the Abstract or Concrete classes which

> implement this interface. But we see two Intefaces in

> java.util package with same method signatures... so

> there should be some reason why they do it that way...

You are completely correct. There are lots of things that cannot be encapsulated in a method definition (eg. that Collection.iterator() is backed by the underlying collection) alone.

An interface is a contract - such a contract combines both the method signatures and the documentation. You could easily write a Set implementation which broke this contract, just as half of Apache commons breaks the "backing" contracts!

oxbow_lakesa at 2007-7-16 1:27:27 > top of Java-index,Core,Core APIs...