What does <E> mean in Collection<E>?

collections, generics, java

Solution

It's the use of generics. Check this intro out. And then don't forget to read this tutorial.

An excerpt follows (which compares the use of a cast versus the use of generics):

When you see the code <Type>, read it as “of Type”; the declaration above reads as “Collection of String c.” The code using generics is clearer and safer. We have eliminated an unsafe cast and a number of extra parentheses. More importantly, we have moved part of the specification of the method from a comment to its signature, so the compiler can verify at compile time that the type constraints are not violated at run time. Because the program compiles without warnings, we can state with certainty that it will not throw a ClassCastException at run time. The net effect of using generics, especially in large programs, is improved readability and robustness.

For example, the interface of a List is

public interface List<E> { 
    void add(E x);
    Iterator<E> iterator();
}

This means you can build a list whose contents are all of the same explicit type (not only of type Object), even if you have defined the type yourself. So, if you create a Name class you can write

List<Name> nameList = new ArrayList<>();

and then fill it with Name instances and directly retrieve Name instances from it without having to cast or otherwise worry about it because you'll always get either a Name instance or null back, never an instance of a different type.

More importantly, you cannot insert anything different from a Name instance in such a List, because it will fail at compile time.

nameList.add(false); //Fails!
nameList.add(new Name("John","Smith")); //Succeeds supposing Name has a 
                                        //firstName, lastName constructor

Problem

What meaning has `<E>` on the code `Collection<E>`?

Original source