How do I create a generic collections?

In this example you will learn how to create a generic collections. Using a generic collections you can define the type of object stored in the collections. Most of the time when you are working with a collection you will store the same kind of object in it. So it is safer to work if you know what to store in the collection and what to expect when try to read some data from it without worried of creating a runtime error in your code because you place a wrong object in the collection.

Using the generic type mechanism introduced since JDK 5.0 you can declare a collections with reference to the given type. You define the type inside an angle brackets after the variable declaration and after the variable instantiation. In JDK 7.0 you can simplify this using the diamond operator. For example is you want to create a collection that stored Customer objects the declaration and instantiation code will looks like the snippet below.

List<Customer> customers = new ArrayList<Customer>();

In JDK 7.0 you can write it without repeating the type on the instantiation part.

List<Customer> customers = new ArrayList<>();

Creating a collection object in this way will tell to compiler that you want to allow only to add objects of type Customer to be the content of the collection. It means the add method will take Customer as the argument and the get method will return Customer object. If you trying to add for example Product object or Seller object the compiler will give you a compile time error.

Let’s see a complete code snippet for this example:

package org.kodejava.example.generics;

import java.util.ArrayList;
import java.util.List;

public class GenericCollection {
    public static void main(String[] args) {
        List<String> customers = new ArrayList<>();
        customers.add(new Customer());
        customers.add(new Customer());

        //customers.add(new Product()); // Compile time error!

        // No cast operator required.
        Customer c = customers.get(0);

        for (Customer customer : customers) {
            System.out.println(customer);
        }
    }
}

Another thing that you can see from the code above is that you don’t have to use cast operator anymore when obtaining data from the collection. On JDK prior to 5.0 version you will typically have to use the instanceof operator to see what type of object returned by the collection and you have to cast it. You don’t have to do this anymore when using generic. You can also see how your code become simpler and more readable when you iterate through the contents of the collection object.

How do I use Generics in Java programming?

In this post we are going to talk about one of Java programming language feature called as generics. Generics is a feature introduced in JDK 5.0. The generics feature allow you to abstract over types. What does it means? It means that you can create a class or method that can work for a type assigned to it. You’ll see a lot of use of generics when you are working with Java Collections. But of course generics can be used for doing other things in you program.

To illustrate this feature let us begin by creating a code when the generics is not yet available in Java to see what problem it trying to solve.

List data = new ArrayList();
data.add("John Doe");
String name = (String) data.get(0);

Here are the things we can see from the code above. First we create an ArrayList and call it data. This variable actually can hold any Java objects in it. On the second line we add a string to this list. And finally on the third line we get the data back from the list. One thing you see here is that you need to cast the object read out from the list. Because the list doesn’t not know what type to return other than java.lang.Object.

If you look to the definition of the List’s add() and get() method prior to JDK 5.0 you’ll see that the add() method will accept Object as argument and the get() method also returns Object.

Now, let say your friend try to use your class and he try to add another object the the list. He add the following lines.

data.add(new Date());
String name = (String) data.get(0);

This code will actually compiled just fine. The add() method will work because Date extends Object. And the get() method will also compiled without any error. But when we execute the program we will get a runtime error saying that it cannot cast a Date object into type of String.

Exception in thread "main" java.lang.ClassCastException: java.util.Date cannot be cast to java.lang.String

So how do you protect your friend from making this mistake? You can use generics. You can tell what the list should store by defining the type for the list. This way you will get a compile time check to make sure that you are adding the correct data to the list. So, your code will look like the following.

List<String> data = new ArrayList<String>();
data.add("John Doe");
String name = data.get(0);

In this generic version of the code snippet you see that now we declare the list to store an object of type String. If you tried to add a Date into the list you’ll get a compile time error. Your IDE will mark the line as error. The other benefit is that you don’t have to do the cast anymore. Generics reduces the clutters in your code by removing the unwanted cast operator from your code.

Actually if you are working on the JDK 7, you can simplify the variable declaration using diamond operator. So you can write it like.

List<String> data = new ArrayList<>();

I hope this will give you the basic understanding of generics and how to use it in your day to day working with your Java projects.