I cannot find the reason for the warning of “unverified or unsafe operations” in Java

according to the headline, I am struggling to find the reason for the warning of “unverified or unsafe operations” 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 , in particular, as a result of a method call, I get the warning "unchecked yadda yadda":

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

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

I tried and tried to figure out what the problem is, and I'm completely at a dead end. The problem is whether I use Sets or Lists. Why will the set returned by getSet be different from Set in the first example?

, - , !: (

+3
5

.

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

Generics, Sun (PDF).

+4

public Set getSet()

public Set<String> getSet()

raw Set Set<String>, , , .

, String, Integer T.

+2
public Set<String> getSet()  // You need to change the signature to this.
{
    Set<String> set = new HashSet<String>();
    return set;
}
+1
source

You forgot to correctly declare the return type of your getSet ().

You have:

public Set getSet() {

whereas you want to return Set<String>as follows:

public Set<String> getSet() {
0
source

Your method returns Set, not Set<String>, therefore, when you assign Set<String> mySet = Set;This is an unverified operation.

0
source

Source: https://habr.com/ru/post/1745040/


All Articles