我找不到“未经检查或不安全的操作”的原因Java 中的警告

发布于 2024-09-01 09:16:42 字数 690 浏览 12 评论 0原文

根据标题,我正在努力寻找某些代码中“未经检查或不安全操作”警告的原因。

如果我有以下代码,它会在没有任何警告的情况下进行编译:

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

现在,如果我更改 mySet 的来源,特别是作为方法调用的结果,我会收到“unchecked yadda yadda”警告

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

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

:我已经尝试了很多次来找出问题所在,但我完全被难住了。无论我使用集合还是列表,问题都存在。为什么 getSet 方法返回的 Set 与第一个示例中的 Set 有所不同?

任何帮助将不胜感激,因为虽然警告不是世界末日,但它却让我烦恼不已! :(

问候

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

相权↑美人 2024-09-08 09:16:42

您需要声明返回参数化类型的方法。

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

要了解有关泛型的更多信息,请查看 有关该主题的 Sun 教程 (PDF)

You need to declare the method to return the parameterized type.

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

To learn more about Generics, check the Sun tutorial on the subject (PDF).

别靠近我心 2024-09-08 09:16:42

解决方案是将您的方法签名从 更改

public Set getSet()

public Set<String> getSet()

您正在尝试将原始 Set 分配给 Set,这本质上是不安全的,因为前者可以保存以下值:后者不能。

您还可以尝试参数化该方法,以便它可以与 StringInteger 或任何其他类型 T 配合使用。

The solution is to change your method signature from

public Set getSet()

to

public Set<String> getSet()

You are trying to assign a raw Set to a Set<String>, which is inherently unsafe because the former can hold values that the latter can't.

You can also try parameterizing the method, so that it will work with String, Integer, or any other type T.

月下凄凉 2024-09-08 09:16:42
public Set<String> getSet()  // You need to change the signature to this.
{
    Set<String> set = new HashSet<String>();
    return set;
}
public Set<String> getSet()  // You need to change the signature to this.
{
    Set<String> set = new HashSet<String>();
    return set;
}
明天过后 2024-09-08 09:16:42

您忘记正确声明 getSet() 调用的返回类型。

你有:

public Set getSet() {

而你想返回一个像这样的 Set

public Set<String> getSet() {

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

You have:

public Set getSet() {

whereas you want to return a Set<String> like this:

public Set<String> getSet() {
梅倚清风 2024-09-08 09:16:42

您的方法返回 Set,而不是 Set,因此当您分配 SetmySet = Set; 这是一个未经检查的操作。

Your method returns Set, not Set<String>, so when you assign Set<String> mySet = Set; That's an unchecked operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文