使用Apache Collections4 Multikey未经检查的调用意味着什么,我该如何解决?

时间:2017-11-29 18:33:55

标签: java intellij-idea collections

我正在更改HashMap以在类中使用MultiKey。

在:

 HashMap<String, MyObject> map = new HashMap<>();

现在我的密钥取决于2个字符串,所以我正在使用:

 HashMap<MultiKey, MyObject> map = new HashMap<>();
 map.put(key(s1,s2),obj);

 private static MultiKey key(String s1, String s2) {
     return new MultiKey(s1,s2);
 }

IntelliJ突出显示对MultiKey的构造函数调用,并告诉我以下内容:

Unchecked call to 'MultiKey(K,K)' as a member of raw type 'org.apache.commons.collections4.keyvalue.MultiKey
Signal places where an unchecked warning is issued by the compiler.

1 个答案:

答案 0 :(得分:2)

您使用的原始类型比非原始类型提供更少的类型安全性。

private static MultiKey key(String s1, String s2) {
    return new MultiKey(s1,s2);
}

此处返回类型是原始类型MultiKey。尝试将其更改为参数化对应MultiKey<String>

private static MultiKey<String> key(String s1, String s2) {
    return new MultiKey<>(s1,s2);
}

此外,您的地图定义也使用原始类型。将其更改为

Map<MultiKey<String>, Descriptor> map = new HashMap<>();

请注意,在这种情况下,在声明地图变量而不是具体类(Map)时使用接口(HashMap)会更好。