Java: Generics: Generalized Collection
Posted on 2011-05-11
Have a class that wraps a List object, providing some additional functionality. Sometimes I need a class with a List<String>, sometimes with a List<Integer> Would like to generalize the type of List to avoid having multiple classes or multiple List variables. Is there a way to define a single List variable within that class so that a factory class could return that class with it having created a List<String> in one case, and a List<Integer> in another case? And, have an add() method in the wrapper class that could be called with a String when its List was List<String>, but the same method could be called with an Integer when the List was created as List<Integer>? I've tried things like List<T> and List<?>, but never seem to quite get it to compile. I understand generics when which using fixed designations like <String> and <Integer>, but using <T> and <?> still baffles me.