As of JDK 5.0, 1.5.0 java.util however, the collection classes are Generic Classes with type parameters.
From Core Java, Volume I Chapter 13:
Object variables and casts. Generics are particularly useful for collection classes,
such as the ubiquitous ArrayList.Generics are -at least on the surface- similar to templates in C++. ...
Generic programming means to write code that can be reused for objects of many different types. For example, you don't want to program separate classes to collect String and File objects.
And you don't have to—the single class
Before JDK 5.0, generic programming in Java was always achieved with inheritance. The
ArrayList collects objects of any class. This is one example of generic programming.
ArrayList (and Vector class) simply maintained an array of Object references:
This approach has two problems. A cast is necessary whenever you retrieve a value:
get to a String will cause an error.
Discuss Collections Framework Overview: Interfaces ( Collection , Set, List, Queue, etc.), AbstractClasses (
AbstractCollection,
AbstractList,
AbstractSequentialList,
AbstractSet,
AbstractQueue,
AbstractMap), Concrete Classes
| Collection Type | Description | |
| ArrayList | An indexed sequence that grows and shrinks dynamically | |
| LinkedList | An ordered sequence that allows efficient insertions and removal at any location | |
| HashSet | An unordered collection that rejects duplicates | |
| TreeSet | A sorted set | |
| EnumSet | A set of enumerated type values | |
| LinkedHashSet | A set that remembers the order in which elements were inserted | |
| PriorityQueue | A collection that allows efficient removal of the smallest element | |
| HashMap | A data structure that stores key/value associations | |
| TreeMap | A map in which the keys are sorted | |
| EnumMap | A map in which the keys belong to an enumerated type | |
| LinkedHashMap | LinkedHashMap A map that remembers the order in which entries were added | |
| WeakHashMap | A map with values that can be reclaimed by the garbage collector if they are not used elsewhere | |
| IdentityHashMap | A map with keys that are compared by ==, not equals | |
Example of creation and use of ArrayList here
Again, as of JDK 5.0 however, the collection classes are Generic Classes with type parameters.
Example of using the parameter for a LinkedList of String
If you use older versions of
of Java, you drop the type parameter and replace the generic types with the Object type.
Objects. Since these Collections are potentially filled with Objects of different types, one may need to use the Java languageinstanceof operator
or Classes method isInstance(Object obj) to determine what the class really was in order to cast it back to
what it had originally been.Note also that primitive data types would have to be in their Wrapper Classes so that they would be Objects in order to be inserted.
Collections Tutorial with
Iterable Interface. The
Collection interface extends the Iterable Interface.
See also Java in a Nutshells link on collections
From Generics Tutorial:
The most commonly used type parameter names are:
Oh, and since it is in this online book too - about the question in class about Java and memory leaks.