I would in some way store many different type in a HashMap, but in a
way that when i extract them they will be well typed (and not an
object).
Short answer is: this is not possible. And type erasure is not the reason: it simply does not make much sense to have this information at compile time. Suppose that Java compiler had some kind of mechanism to keep track of the types of objects stuffed into a HashMap. Then it also would have to cope with something like this:
HashMap<Integer, Value<?>> myMap = new HashMap<Integer, Value<?>>;
if (Math.random() > 0.5) {
myMap.put(0, new Value<String>("hello world"));
} else {
myMap.put(0, new Value<MacaroniExtruder>(new MacaroniExtruder()));
}
[whatDoYouWantToWriteHere?] value = myMap.get(0);
What you want is probably rather something like "type-union". Unfortunately, it does not exist neither in Java, nor in Scala (where we have case-classes, which are almost just as good for that). So all you can do is basically:
A) Use polymorphism, define a proper interface, so that the concrete implementations become completely irrelevant, and work with that interface as second argument to your hash Map (preferred).
B) Use instanceof and long if-else switches (ugly)