Jenkov Tutorials : Java Generics

Angelika Langer Generics FAQs

Generic Classes :

public class GenericFactory<T> {
    Class myClass;
    
    public GenericFactory(Class myClass){
        this.myClass = myClass;
    }
    
    public T instance(){
        return (T) this.myClass.newInstance();
    }
}

GenericFactory<SomeClass> someClassFactory = new GenericFactory<SomeClass>(SomeClass.class); 
SomeClass classInstance = someClassFactory.instance();

Generic Method :

public static <T> T addAndReturn(T element, Collection<T> myCollection){
    myCollection.add(element);
    return element;
}
String str = "someString";
List<String> collection = new ArrayList<>();
String addedElement = addAndReturn(str, collection);

Generic Wildcard

Unknown Wildcard

List<?> listA = new ArrayList<A>();

Only read can be performed.

public void processElements(List<?> myList){
    for(Object element : myList){
        System.out.println(element);
    }
 }

Extend Wildcard

List< ? extends A> listExtendA = new ArrayList<A>()

We can read the collection and cast its elements to A.

 public void processElements(List<? extends A> elements){
    for(A a : elements){
       System.out.println(a.getValue());
    }
 } 

Super Wildcard

List<? super A> listSuperA = new ArrayList<A>()

It is safe to insert A or subclasses of A

 public static void insertElements(List<? super A> list){
     list.add(new A());
     list.add(new B());
     list.add(new C());
 }

More details on Extends and Super.

Extends

The wildcard declaration of List<? extends Number> foo3 means that any of these are legal assignments:

List<? extends Number> foo3 = new ArrayList<Number>();  // Number "extends" Number (in this context)
List<? extends Number> foo3 = new ArrayList<Integer>(); // Integer extends Number
List<? extends Number> foo3 = new ArrayList<Double>();  // Double extends Number
  1. Reading - Given the above possible assignments, what type of object are you guarenteed to read from List foo3:
    • You can read a Number because any of the lists that could be assigned to foo3 contain a Number or a subclass of Number.
    • You can’t read an Integer because foo3 could be pointing at a List.
    • You can’t read a Double because foo3 could be pointing at a List.
  2. Writing - Given the above possible assignments, what type of object could you add to List foo3 that would be legal for all the above possible ArrayList assignments:
    • You can’t add an Integer because foo3 could be pointing at a List.
    • You can’t add a Double because foo3 could be pointing at a List.
    • You can’t add a Number because foo3 could be pointing at a List.

You can’t add any object to List<? extends T> because you can’t guarantee what kind of List it is really pointing to, so you can’t guarantee that the object is allowed in that List. The only “guarantee” is that you can only read from it and you’ll get a T or subclass of T.

Super

The wildcard declaration of List<? super Integer> foo3 means that any of these are legal assignments:

List<? super Integer> foo3 = new ArrayList<Integer>();  // Integer is a "superclass" of Integer (in this context)
List<? super Integer> foo3 = new ArrayList<Number>();   // Number is a superclass of Integer
List<? super Integer> foo3 = new ArrayList<Object>();   // Object is a superclass of Integer
  1. Reading - Given the above possible assignments, what type of object are you guaranteed to receive when you read from List foo3:
    • You aren’t guaranteed an Integer because foo3 could be pointing at a List or List.
    • You aren’t guaranteed an Number because foo3 could be pointing at a List.
    • The only guarantee is that you will get an instance of an Object or subclass of Object (but you don’t know what subclass).
  2. Writing - Given the above possible assignments, what type of object could you add to List foo3 that would be legal for all the above possible ArrayList assignments:
    • You can add an Integer because an Integer is allowed in any of above lists.
    • You can add an instance of a subclass of Integer because an instance of a subclass of Integer is allowed in any of the above lists.
    • You can’t add a Double because foo3 could be pointing at a ArrayList.
    • You can’t add a Number because foo3 could be pointing at a ArrayList.
    • You can’t add a Object because foo3 could be pointing at a ArrayList.
public class Collections { 
  public static <T> void copy(List<? super T> dest, List<? extends T> src) 
  {
      for (int i=0; i<src.size(); i++) 
        dest.set(i,src.get(i)); 
  } 
}
Java