Java: Set interface and Collection interface differences

前端 未结 4 2159
时光取名叫无心
时光取名叫无心 2021-02-12 10:43

I just looked up the Set interface and found that it mostly (or completely) only redeclares functions which are already in the Collection interface.

4条回答
  •  你的背包
    2021-02-12 11:00

    They are redeclared because, even if the names are same, they have different meaning. The add method in the Set is a specific implementation of the generic add method in Collection.

    The intention is to explicitly specify that the add method os the Set is very different from the add method of Collection.

    Why not just define the Set interface as:

    public interface Set extends Collection {} 
    

    If it has been done this way, there would be no place where the contract of a Set can be specified. How would I know that by implementing the add method of the Set, I should not allow duplicates?

提交回复
热议问题