Java Generics Bounded Wildcard for List collection
import java.util.LinkedList; import java.util.List; public class Main { public static void main(String... args) { // Wildcard bounded parameterized types ////from ww w . j a v a2s. co m List<? extends Number> cList; cList = new LinkedList<Number>(); cList = new LinkedList<Integer>(); cList = new LinkedList<Double>(); // we can get(), but we can't add() with "? extends" cList.add(new Integer(5)); // add() not allowed; cList could be LinkedList<Double> Number cNum = cList.get(0); List<? super Integer> dList; dList = new LinkedList<Integer>(); dList = new LinkedList<Number>(); dList = new LinkedList<Object>(); // we can do limited add()'s, but we can't get() with "? super" Integer dInt = dList.get(0); // get() not allowed; dList.get(0) could be a Number or Object dList.add(new Integer(5)); // OK Number dNum = new Double(7); dList.add(dNum); // won't compile; dList could be LinkedList<Integer> } }