3

Any ideas on the correct method of typecasting an Object out of a getAttribute request from a JSP page HttpServletRequest?

I have googled but it seems that the common solution is just sticking suppresswarnings in your code... Something I would very much like to avoid.

I currently have:

HashMap<String, ArrayList<HashMap<String, String>>> accounts = (HashMap<String, ArrayList<HashMap<String, String>>>)request.getAttribute("accounts");

And the complier is giving me this warning: Unchecked cast from Object to HashMap

Thanks in Advance!!

MB.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Tobias M
  • 1,308
  • 3
  • 18
  • 23

2 Answers2

1

Yeah, either you need to cast or suppress the warning. In case you decide to suppress the warning, I recommend you to put a comment, too.

Similar thread, and another.

Community
  • 1
  • 1
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
1

I ended up using JSTL tags to iterate over the collection, this allowed for an elegant solution not requiring suppression of unchecked warnings.

Tobias M
  • 1,308
  • 3
  • 18
  • 23