his Java generics tutorial is to introduce basic terminologies related to generics. They are demonstrated with example code and this tutorial is for beginners only.
Java Generics Example
Before we go into detail let us see an example for generics in Java. This is to recall and continue from our previous
tutorial for introduction to generics. In the following example we are using the generics type implemented for collections. This shows how we can use a generic type.
package com.javapapers.java;
import java.util.ArrayList;
import java.util.List;
public class JavaGenericsExample {
public static void main(String args[]) {
List<String> animalsList = new ArrayList<String>();
animalsList.add( "Lion" );
animalsList.add( "Crocodile" );
animalsList.add( "Hyena" );
for (String animal : animalsList) {
System.out.println(animal);
}
}
}
|
Generic Type
A generic type is the definition of a generic class. The generic type definition will have a placeholder for a type. Following are examples from Java JDK API for generic types
List – JDK Source
public interface List<E> extends Collection<E> {
...
boolean add(E e);
...
}
|
ArrayList – JDK Source
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
public boolean add(E e) {
ensureCapacityInternal(size + 1 );
elementData[size++] = e;
return true ;
}
...
}
|
Parameterized Type
When a generic type is instantiated, it is called parameterized type. We supply a class/type as parameter for placeholder and create a new object for a generic type. In the above java generics example, we have instantiated a List object by supplying the parameter argument as String.
List<String> animalsList = new ArrayList<String>();
|
Wildcard Type
Using a generic type with wildcards is wildcard type. In the below example, we have written a new method using generics which uses the wildcard type. We have not give any actual parameter as argument in the topTwo method. We have used a wildcard ? as argument. This states that we can call this method by passing a List containing any type.
package com.javapapers.java;
import java.util.ArrayList;
import java.util.List;
public class GenericsWildcardType {
public static void main(String args[]) {
List<String> animalsList = new ArrayList<String>();
animalsList.add( "Lion" );
animalsList.add( "Crocodile" );
animalsList.add( "Hyena" );
List<?> topTwoAnimals = topTwo(animalsList);
System.out.println(topTwoAnimals);
}
public static List<?> topTwo(List<?> list) {
return list.subList( 0 , 2 );
}
}
|
Bounded Wildcard Type
We can restrict the wildcard type arguments as applicable to only a specific subtype or supertype. Instead of any type at the place of the wildcard ?, it is restricted to the sub or super classes of a specific class. For example, in the above wildcard type example we can add the keyword ‘extends’ as below and restrict it to a subtype only. This is known as upper bound. Similarly ‘super’ can be used to restrict it to a supertype and is known as lower bound.
public static List<?> topTwo(List<? extends String> list) {
return list.subList( 0 , 2 );
}
|
Generics Custom Type
Following is an example for Java generics demonstrating generic type, parameterized type, wildcard type and bounded wildcard type.
package com.javapapers.java;
public class CustomGenericType {
public static void main(String args[]) {
Cup<Drink> cup = new Cup<Drink>();
Cup<Tea> cupOfTea = new Cup<Tea>( new Tea());
System.out.println( "Java Generics Rocks: " + cupOfTea.pour());
cup.fillFrom(cupOfTea);
System.out.println( "Java Generics Poured: " + cup.pour());
}
}
class Drink {
@Override
public String toString() {
return "empty" ;
}
}
class Tea extends Drink {
@Override
public String toString() {
return "Tea" ;
}
}
class Cup<E> {
String content;
public Cup() {
}
public Cup(E e) {
content = e.toString();
}
public void fillFrom(Cup<? extends Drink> c) {
content = c.pour();
}
public String pour() {
return content;
}
}
|
This Java generics tutorial you have read now is part two of Generics series and there is lot more to come, keep watching.