Tech and Media Labs
This site uses cookies to improve the user experience.




Java Generics Tutorial

Jakob Jenkov
Last update: 2015-07-07

The Java Generics features were added to the Java language from Java 5. Generics add a way to specify concrete types to general purpose classes and methods that operated on Object before. It sounds a bit abstract, so we will look at some examples using collections right away.

Note: Java generics can be used with other classes than the collection clasess, but it is easiest to show the basics of Java generics using collections.

Java Generics Example

The List interface represents a list of Object instances. This means that we could put any object into a List. Here is an example:

List list = new ArrayList();

list.add(new Integer(2));
list.add("a String");

Because any object could be added, you would also have to cast any objects obtained from these objects. For instance:

Integer integer = (Integer) list.get(0);

String string   = (String) list.get(1);

Very often you only use a single type with a collection. For instance, you only keep String's or something else in the collection, and not mixed types like I did in the example above.

With the Java Generics features you can set the type of the collection to limit what kind of objects can be inserted into the collection. Additionally, you don't have to cast the values you obtain from the collection. Here is an example using Java's Generic's features:

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

strings.add("a String");

String aString = strings.get(0);

Nice, right?

Java 7 Type Inference

The Java generics features were updated in Java 7. From Java 7 the Java compiler can infer the type of the collection instantiated from the variable the collection is assigned to. Here is a Java 7 generics example:

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

Notice how the generic type of the ArrayList has been left out. Instead is only the <> written. This is also sometimes referred to as the diamond operator. When you just write a diamond operator as generic type, the Java compiler will assume that the class instantiated is to have the same type as the variable it is assigned to. In the example above, that means String because the List variable has String set as its type.

Java Generics for Loop

Java 5 also got a new for-loop (also referred to as "for-each") which works well with generified collections. Here is an example:

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

//... add String instances to the strings list...

for(String aString : strings){
  System.out.println(aString);
}

This for-each loop iterates through all String instances kept in the strings list. For each iteration, the next String instance is assigned to the aString variable. This for-loop is shorter than original while-loop where you would iterate the collections Iterator and call Iterator.next() to obtain the next instance.

The generic for loop (AKA "for each loop") is described in more detail in the tutorial about the Java generic for loop .

Java Generics for Other Types than Collections

It is of course possible to use Generics for other classes than the Java collections. You can generify your own classes too. Using Java generics in your own classes is described in more detail in the tutorials about generic classes, generic methods and using class objects as type literals.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC