I have a HahMap of Key Value pairs. The key is string but the value can be a mix of any custom object types for example, Person, Exam or XYZ.
so I create a hashmap like this:
Map<String, Object> myMap= new HashMap<String, Object>();
Person p = new Person();
Exam e = new Exam();
XYZ xy = new XYZ();
Now When Loop through the map and get each items like this, I get the warning: unchecked cast
Person p2 = (Person) myMap.get("p");
How to declare the hashmap to avoid the warnings. ?
1) I know I can suppress the warnings, but I don't want to do that.
2)We can avoid the warning, if we add objects of the same type and declare the HashMap like this:
Map<String, Person> myMap= new HashMap<String, Person>();
But my hashmap may contain objects of multiple types.