File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to Cast In Jdk5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How to Cast In Jdk5" Watch "How to Cast In Jdk5" New topic

How to Cast In Jdk5

Xavier Lio

Joined: Oct 07, 2007
Posts: 9
I have a function get a input of Object and I need to convert it to LinkedHashMap so if I use (LinkedHashMap)Object to cast the input the complier report errors. So what can I do to solve it.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
What's the error message? What does the code look like?
Xavier Lio

Joined: Oct 07, 2007
Posts: 9
The error message is
Type Safty : the cast from object to LinkedHashMap<String,String> is actually checking against the erased type LinkedHashMap.

And the code is
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274

Casting to generic types always produces this warning. You can do nothing against it (except suppressing the warning).
The only thing you can do is accepting the warning. And perhaps take the warning as a - ehem - warning...
to check your code if the method returnning the object you cast really returns a map of the type you need.

We are always encountered with this problem e.g. in deserialization, where you get only an object back.

By the way,

welcome to the Ranch, Xavier


all events occur in real time
I agree. Here's the link:
subject: How to Cast In Jdk5
It's not a secret anymore!