使用Java HashMap进行Eclipse警告

plk*_*Mon 5 java eclipse generics hashmap

当我使用以下代码时,Eclipse会说"HashMap是原始类型"

HashMap = new HashMap();
Run Code Online (Sandbox Code Playgroud)

知道什么可能是错的吗?

Pow*_*ord 17

当您使用Java 5或更高版本的非Generic 时,Eclipse会向您发出警告HashMap.

另请参阅:Sun Java教程中的泛型课程.

编辑:其实,在这里,我也举一个例子:

假设我想将某人的名字映射到他们的Person对象:

Map<String, Person> map = new HashMap<String, Person>();
// The map.get method now returns a Person
// The map.put method now requires a String and a Person
Run Code Online (Sandbox Code Playgroud)

这些在编译时检查; 由于Java实现了泛型,因此在运行时丢失了类型信息.