I'd like to create a HashMap
that maps specific class types to one single specific new object.
Later I want to pass the class type and get the reference to that specific object. Simple example:
Map<Class<?>, ?> values = new HashMap<>();
public <T> t get(Class<T> type) {
return values.get(type);
}
//pet and car do not share any interface or parent class
class Pet;
class Car;
//error: not applicable for arguments
values.put(Pet.class, new Pet());
values.put(Car.class, new Car());
usage:
values.get(Pet.class);
How can I create such a generic hashmap and lookup function accordingly?