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?
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.
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.
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.
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.
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.
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.
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With