Java generics for optional single value or collection
Java generics for optional single value or collection I'm trying to define a container for a whole bunch of classes as some parts of the code will make more sense with a collection but other places will make sense with single values. Ideally I'd like to do this: public class AllModes<T> { private T<Car> car; private T<Boat> boat; private T<Train> train; private T<Plane> plane; ...40 more of these... } then I'd like to use the class like: AllModes<List> allModes; AllModes<Optional> oneOfEachMode; But I get the error I get is "The type T is not generic; it cannot be parameterized with arguments " The reason I'm defining these in multiple variables and not a single HashSet based on a superclass is I want to have get methods that return the correct types to avoid consumers of this class needing to cast down everywhere as each object has its own distinct fields. I also considered just storing a single value l...