JKB002 - Generics in Java 1.5 Tutorial

Popular Courses

Browse Our Free Resources

  • whitepapers
  • whitepapers
  • webinars
  • blogs

Our Locations

Training Centres

Vancouver, BC
Calgary, AB
Edmonton, AB
Toronto, ON
Ottawa, ON
Montreal, QC
Hunt Valley

locations map


550 6th Av SW
Suite 475
Calgary, AB
T2P 0S2


821A Bloor Street West
Toronto, ON
M6G 1M1


409 Granville St
Suite 902
Vancouver, BC
V6C 1T2

U.S. Office

436 York Road
Suite 1
Jenkintown, PA

Other Locations

Dallas, TX
Miami, FL

Web Age Solutions Inc.


Collections are typeless in Java. For example, an ArrayList object can contain any Java object. Putting an object in a collection is easy.

ArrayList list = new ArrayList();
CustomerBean cb = new CustomerBean():


Getting an object from a collection requires a type cast.

cb = (CustomerBean) list.get(0);

There in lies the problem with typeless collections. If for some reason, the list contained an instance of a class other than CustomerBean, the get() operation will fail with a type cast exception.

Enter Generics

In Java 1.5, the code above can be written as follows.

ArrayList<CustomerBean> list = new ArrayList<CustomerBean>();
CustomerBean cb = new CustomerBean():


Here, the compiler will ensure that only instances of the CustomerBean class (or subclass there of) are added to the list.

Getting an object from the collection does not require type casting.

cb = list.get(0);

The following is a compilation error.

String str = list.get(0); //Compiler will catch the error

Iteration Using Generic Collection

Easiest way to iterate over a collection is to use the new for syntax.

for (CustomerBean o : list) {

If you wish to use an Interator, it must also use generics.

Iterator<CustomerBean> iter = list.iterator();

while (iter.hasNext()) {
     CustomerBean c =;

Bypassing Compiler Check

It's not that hard to bypass the compiler checking for type correctness. Consider a method as follows.

public static void bypassGenericCheck(ArrayList l) {
     l.add("Mama"); //Compiler gives warning not error

We call this method as follows.

ArrayList<CustomerBean> list = new ArrayList<CustomerBean>();
CustomerBean cb = new CustomerBean():

bypassGenericChec(list); //Add instances other than CustomerBean.

Iterator<CustomerBean> iter = list.iterator();

while (iter.hasNext()) {
     CustomerBean c =; //This line will fail with ClassCastException

In this case, compiler gives a mere warning for the add() call within the bypassGenericCheck() method. The line containing the call will fail with ClassCastException when returns a String.

Creating Custom Collection Types

Certainly, generics can increase the amount of typing. One way to minimize typing is to create a custom collection class. For example, if our application has ArrayList<CustomerBean> appearing in many places, we may decide to develop a simple class as follows.

public class CustomerList extends ArrayList<CustomerBean> {

We can use it as follows.

CustomerList l = new CustomerList();

CustomerBean cb = new CustomerBean();


Email Address: *

Very useful Somewhat useful Not bad Needs many corrections


( * ) = mandatory field