Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does List interface extend Collection interface?

The Collection interface has multiple methods. The List interface extends the Collection interface. It declares the same methods as the Collection interface? Why is this so?

For example

interface Collection extends Iterable
{
     public abstract int size();
 public abstract boolean isEmpty();
 public abstract boolean contains(java.lang.Object);
 public abstract java.util.Iterator<E> iterator();
 public abstract java.lang.Object[] toArray();
 public abstract <T extends java/lang/Object> T[] toArray(T[]);
 public abstract boolean add(E);
 public abstract boolean remove(java.lang.Object);
 public abstract boolean containsAll(java.util.Collection<?>);
 public abstract boolean addAll(java.util.Collection<? extends E>);
 public abstract boolean removeAll(java.util.Collection<?>);
 public abstract boolean retainAll(java.util.Collection<?>);
 public abstract void clear();
 public abstract boolean equals(java.lang.Object);
 public abstract int hashCode();
}

and same methods are also present in List interface:

public interface List extends Collection
{
 public abstract int size();
 public abstract boolean isEmpty();
 public abstract boolean contains(java.lang.Object);
 public abstract java.util.Iterator<E> iterator();
 public abstract java.lang.Object[] toArray();
 public abstract <T extends java/lang/Object> T[] toArray(T[]);
 public abstract boolean add(E);
 public abstract boolean remove(java.lang.Object);
 public abstract boolean containsAll(java.util.Collection<?>);
 public abstract boolean addAll(java.util.Collection<? extends E>);
 public abstract boolean removeAll(java.util.Collection<?>);
 public abstract boolean retainAll(java.util.Collection<?>);
 public abstract void clear();
 public abstract boolean equals(java.lang.Object);
 public abstract int hashCode();
}

Is it a requirement to write these methods again in List if it is already extending the Collection interface?

like image 855
user2602839 Avatar asked Aug 10 '13 10:08

user2602839


People also ask

Does List interface extend collection interface?

The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements. Elements can be inserted or accessed by their position in the list, using a zero-based index. A list may contain duplicate elements.

What does the List interface add to the collection concept?

The List interface in Java provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements.

Why does not the Map interface extend the Collection interface?

Because they are of an incompatible type. List, Set and Queue are a collection of similar kind of objects but just values where a Map is a collection of key and value pairs.

Does List implement Collection interface?

A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements. In addition to the operations inherited from Collection , the List interface includes operations for the following: Positional access — manipulates elements based on their numerical position in the list.


4 Answers

They're re-written so that they can be documented, in order to specify how the List refines the contract of these methods compared to the contract specified in the Collection interface.

For example, the add() method in List is documented to specify that the element is added to the end of the list. This can't be specified in Collection, since a Collection doesn't have a beginning and an end.

like image 161
JB Nizet Avatar answered Oct 12 '22 14:10

JB Nizet


JavaDoc and API contracts change somewhat/ or become more specific, as you move down the inheritance heirarchy.

List re-declares these methods & gives them more specific JavaDoc.

like image 20
Thomas W Avatar answered Oct 12 '22 13:10

Thomas W


Just for convenience.

Same mentioned in Docs

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

like image 6
Suresh Atta Avatar answered Oct 12 '22 14:10

Suresh Atta


Collection<T> is just a group of item. In itself it doesn't have any more requirements than holding references to the many items that are its members.

In the basic java api there is two main types of collections: List<T> and Set<T>.

List<T> have the extra requirement to maintain a certain order (insertion order, sorted order, ...) for all its items. So that if you request item N the list will always return the same item for N.

Set<T> does not offer any guarantee on order but offers guarantee on uniqueness of the items. An item A cannot be added twice to a Set, or will appear only once in a set.

You should acquaint yourself with the practice of "marker" interface. Serializable is one of those, and generally the basic example when talking about this. And List<T> and Set<T> are declared as such, they mark a collection as one or the other in order to inform the programmer of the behaviour they can expect from the collection they receive.

Please refer to Item 37 (Chapter 6) of "Effective Java" for a very good explanation on how this is better than using annotations.

And there is also the fact that myCollection instanceof MyInterface is faster than myCollection.getClass().isAnnotationPresent(MyAnnotation.class) or myCollection.getClass().getAnnotation(MyAnnotation.class) != null.

like image 3
le-doude Avatar answered Oct 12 '22 14:10

le-doude