<<< Common collection classes | Index | Example of Java Generic Class >>> |
CollectionClass<ElementType> collectionName = new CollectionClass<ElementType>();
Note that
CollectionClass<ElementType>
syntax specifies that the interface is generic.
Generic interface requires that the type of object contained in the collection (the element type) is specified when the CollectionClass is instantiated.
In other words, the type of the element of the container becomes known to the compiler, making your program much safer.
For example,
List<String> list = new ArrayList<String>(); list.add("hello"); String s = list.get(0);
<<< Common collection classes | Index | Example of Java Generic Class >>> |