Take a look at the JavaDocs for List.
A lot of them have lines like this, showing that they're from the Collection
interface:
Specified by:
size in interface Collection<E>
The ones with no "specified by" section are:
E get(int index)
E set(int index,
E element)
void add(int index,
E element)
E remove(int index)
int indexOf(Object o)
int lastIndexOf(Object o)
ListIterator<E> listIterator()
ListIterator<E> listIterator(int index)
List<E> subList(int fromIndex,
int toIndex)
Where E
is the Type
specified in List<Type>
.
Basically, it's a bunch of things related to indexes -- because not all Collections
have an index, or an order at all for that matter, and a couple related to special iterators, and subList
.
The advantage to using Collection
in method signatures is that you don't force your users to use one kind of collection (some users can use a Set
, some can use a List
, etc.). This only makes sense if you don't need the methods that `List gives you.
In this example, I'm not using any List
-specific methods:
/**
* Simple example, adds "1" to the Collection
*/
public static void addOne(Collection in) {
in.add(1);
}
There's no reason to force users of this method to only pass it a list, because the only method we're calling on it (add
) is available in all Collection
s.