I started working with Java recently and am still confused when working with generic types. The following is a simplified scenario in which some problems arise.
I have a class that contains a map using the class type as a key and a collection of objects of this class:
public class GenericListInside {
private Map<Class<?>, List<?>> mapping = new HashMap<>();
public <T> void addListing(Class<T> clazz, List<T> object) {
mapping.put(clazz, object);
}
}
I can call addListing without problems:
GenericListInside gli = new GenericListInside();
List<Foo> list = new ArrayList<>();
gli.addListing(Foo.class, list);
Now I decided to create a class to provide a fluid interface. Sort of:
with(Foo.class).use(list);
Then I came with:
public class FluidInserter<T> {
Class<T> clazz;
GenericListInside gli = new GenericListInside();
public FluidInserter with (Class<T> clazz) {
this.clazz = clazz;
return this;
}
public <T> void use(List<T> list) {
gli.addListing(clazz, list);
}
}
But when I try to compile, I get:
: (18, 12) java: addListing util.GenericListInside .
: java.lang.Class, java.util.List
: java.lang.Class, java.util.List
: (s)
inferred: T
(s): T, T
... - , ?