Java: Set interface and Collection interface differences

前端 未结 4 1752
予麋鹿
予麋鹿 2021-02-12 10:39

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:06

    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?

提交回复
热议问题