Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why methods declared in Collection are duplicated in List Interface also? [duplicate]

Tags:

java

list

Is there any specific reason(other than what is mentioned below) why all the methods declared in java.util.Collection is duplicated in java.util.List interface?

According to java.util.List Api:

The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. Declarations for other inherited methods are also included here for convenience.

Just for the sake of addtional documentation(stipulations) is it good to repeat the method declarations like that?

like image 275
Mahendran Avatar asked Dec 18 '13 09:12

Mahendran


People also ask

Why list allow duplicate values in Java?

List allows duplicates while Set doesn't allow duplicate elements . All the elements of a Set should be unique if you try to insert the duplicate element in Set it would replace the existing value. List permits any number of null values in its collection while Set permits only one null value in its collection.

Which interface does not allow duplicate elements in a collection?

A Set is a Collection that cannot contain duplicate elements. It models the mathematical set abstraction. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

Which interface allows duplicates?

Unlike sets, lists typically allow duplicate elements. Option D is wrong. A collection is also known as a sequence. The user of this interface has precise control over where in the list each element is inserted.

Which collection doesn allow duplicates?

ArrayList allows duplicate values while HashSet doesn't allow duplicates values.


2 Answers

Just for the sake of addtional documentation(stipulations) is it good to repeat the method declarations like that?

How would you do it differently? What is the downside?

Yes, this really is the simplest way to do that.

like image 198
Peter Lawrey Avatar answered Oct 17 '22 17:10

Peter Lawrey


The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. Declarations for other inherited methods are also included here for convenience.

For e.g the Javadoc of add method inside List interface reads out to:

Appends the specified element to the end of this list (optional operation).

Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.

whereas Javadoc of add method inside Collection interface reads out to:

Ensures that this collection contains the specified element (optional operation). Returns true if this collection changed as a result of the call. (Returns false if this collection does not permit duplicates and already contains the specified element.)

Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false). This preserves the invariant that a collection always contains the specified element after this call returns.

like image 1
Aditya Avatar answered Oct 17 '22 18:10

Aditya