Java Generics Type Safety warning with recursive Hashmap

Posted by GC on Stack Overflow See other posts from Stack Overflow or by GC
Published on 2010-03-14T21:13:30Z Indexed on 2010/03/14 21:15 UTC
Read the original article Hit count: 468

Hi,

I'm using a recursive tree of hashmaps, specifically Hashmap map where Object is a reference to another Hashmap and so on. This will be passed around a recursive algorithm:

foo(String filename, Hashmap<String, Object> map)
{
    //some stuff here
    for (Entry<String, Object> entry : map.entrySet()) 
    {
       //type warning that must be suppressed
       foo(entry.getKey(), (HashMap<String, Object>)entry.getValue());
    }
}

I know for sure Object is of type Hashmap<String, Object> but am irritated that I have to suppress the warning using @SuppressWarnings("unchecked").

I'll be satisfied with a solution that does either a assert(/*entry.getValue() is of type HashMap<String, Object>*/) or throws an exception when it isn't. I went down the Generics route for compile type safety and if I suppress the warning then it defeats the purpose.

Thank you for your comments, ksb

© Stack Overflow or respective owner

Related posts about java

Related posts about hashmap