什么是未经检查和不安全的操作?

Rom*_*man 5 java generics collections hashset

我有以下代码:

private static final Set<String> allowedParameters;
static {
    Set<String> tmpSet = new HashSet();
    tmpSet.add("aaa");
    allowedParameters = Collections.unmodifiableSet(tmpSet);
}
Run Code Online (Sandbox Code Playgroud)

它导致:

Note: mygame/Game.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Run Code Online (Sandbox Code Playgroud)

当我使用建议的选项重新编译时,我看到一个指针(^)指向前面的"新" HashSet();.

有谁知道这里发生了什么?

Jac*_*son 9

是的,你正在创建一个新的HashSet而不说明它应该包含哪个类,然后断言它包含字符串.将其更改为

 Set<String> tmpSet = new HashSet<String>();
Run Code Online (Sandbox Code Playgroud)