I can't find the cause of an "unchecked or unsafe operations" warning in Java.

Posted by Peter on Stack Overflow See other posts from Stack Overflow or by Peter
Published on 2010-05-12T02:48:18Z Indexed on 2010/05/12 2:54 UTC
Read the original article Hit count: 241

Filed under:
|
|

Hello, as per the title I am struggling to find the cause of an "unchecked or unsafe operations" warning in some code.

If I have the following code, it compiles without any warnings:

public void test()
{
     Set<String> mySet = new HashSet<String>();
     Set<String> myNewSet = mySet;
     //do stuff
}

Now, if I change where mySet comes from, specifically as the result of a method call, I get the "unchecked yadda yadda" warning:

public void test()
{
    Set<String> myNewSet = this.getSet();
    //do stuff
}

public Set getSet()
{
    Set<String> set = new HashSet<String>();
    return set;
}

I have tried and tried to work out what the problem is and I am completely stumped. The issue is present whether I use Sets or Lists. Why would the Set returned by the getSet method be any different to the Set in the first example?

Any help would be greatly appreciated as while the warning isn't the end of the world, it is bugging the hell out of me! :(

Regards

© Stack Overflow or respective owner

Related posts about java

Related posts about generics