1

I have a jsp scriplet where I wrote this code

Object obj=session.getAttribute("ListOfCountry");

HashMap<Integer, String> hm = (HashMap<Integer, String>) obj;

But it says "Type safety: Unchecked cast from Object to (HashMap <Integer, String>)" error. Any idea to resolve it?

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91

1 Answers1

-1

NOTE: First, you need to check if obj is a HashMap or not.

Replace this:

HashMap<Integer, String> hm = (HashMap<Integer, String>) obj;

With this:

if(obj instanceof HashMap) {
    HashMap<Integer, String> hm = (HashMap<Integer, String>) obj;
}

Alternative Solution is that you can use @SuppressWarnings("unchecked") to stop that warning.

Suryakant Bharti
  • 673
  • 1
  • 6
  • 24