Java HashMap 类型不是通用的;它不能用参数 <String, Integer> 参数化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18984915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The type HashMap is not generic; it cannot be parameterized with arguments <String, Integer>
提问by Shane
This is a strange error i am getting today when i try to implement a Map as below.
这是我今天在尝试实现如下 Map 时遇到的一个奇怪错误。
Map<String, Integer> cache = new HashMap<String, Integer>();
I am using JDK 1.7and not sure why this error has been coming and changing the above line by adding cast removes the error. I looked at the related posts in stackoverflow before posting this question seems to be strange issue.
我正在使用JDK 1.7,但不确定为什么会出现此错误,并通过添加强制转换来更改上述行以消除错误。在发布这个问题之前,我查看了 stackoverflow 中的相关帖子,这似乎是一个奇怪的问题。
Map<String, Integer> cache = (Map<String, Integer>) new HashMap();
采纳答案by Alfredo Osorio
Check you are actually using java.util.HashMapand java.util.Mapin your imports.
检查您是否在导入中实际使用java.util.HashMap和java.util.Map。
回答by Adam Arold
I'm sure that you are importing the wrong HashMap
. You have to use the java.util
packages for the code you presented there.
我确定您导入了错误的HashMap
. 你必须使用java.util
你在那里提供的代码的包。
We can't help you any further without seeing your import statements.
如果没有看到您的导入语句,我们将无法为您提供任何进一步的帮助。
回答by allen gay
I did something really stupid to get this error. You might check. I named my class "HashMap" lol. You might check that.
我做了一些非常愚蠢的事情来得到这个错误。你可能会检查。我将我的课程命名为“HashMap”,哈哈。你可能会检查一下。
回答by Shwetank Rastogi
I have also gone through the same error but it was resolved just by changing some properties of the project:
我也遇到了同样的错误,但仅通过更改项目的某些属性就解决了:
- Right-click on your project
- Click on
Properties
- Select
Java Build Path
from right-hand side panel - Select
Order and Export
tab - Click on your
JRE System Library or JDK Library
- Click on
Up
button and move it to first position - Click
Ok
- Clean & build your project.
- 右键单击您的项目
- 点击
Properties
- 选择
Java Build Path
从右侧面板 - 选择
Order and Export
标签 - 点击你的
JRE System Library or JDK Library
- 单击
Up
按钮并将其移动到第一个位置 - 点击
Ok
- 清理并构建您的项目。
Do repeat this for all other dependents project as well, if you have dependencies.
如果您有依赖项,请对所有其他依赖项也重复此操作。
It resolved my issue because previously the Java files were picking other libraries and packages not from JRE package as it was ordered set in last priority.
它解决了我的问题,因为以前 Java 文件选择了其他库和包,而不是从 JRE 包中选择的,因为它是按顺序设置的。
回答by Neeraj Pandey
If none of the above solution works ,the only possible reason you are getting this error is because you might have named your class_name similar to an already existing class which is either in util or lang library .
如果上述解决方案均无效,则您收到此错误的唯一可能原因是您可能将 class_name 命名为类似于 util 或 lang 库中的现有类。
回答by Ankush kumar
@Neeraj Pandey, you have exactly got it right and suggested an absolutely correct answer.
@Neeraj Pandey,您完全正确并提出了绝对正确的答案。
Never name or keep your class name same as any class which is predefined in Java Util. For example : In Java HashMap is predefined class and if create a new class with same name i.e HashMap then its obvious to get such error.
永远不要命名或保持您的类名与 Java Util 中预定义的任何类相同。例如:在 Java HashMap 中是预定义的类,如果创建一个同名的新类,即 HashMap,那么很明显会得到这样的错误。
So avoid doing such mistakes.
所以避免犯这样的错误。
回答by pnkj
Recently, I have met the same problem.Almost I have found all the answers, but I failed.
I had imported java.util.HashMapand java.util.Mapor set the JRE System Libraryto the top.But in vain.
最近遇到了同样的问题,几乎找到了所有的答案,但是失败了。
我已经导入了java.util.HashMap和java.util.Map或将JRE 系统库设置为顶部。但徒劳无功。
At last, I tried to change my Class name from HashMapto HashMapDemo. Now, I just want to tell you that your Class name may be one of the java.util.*, you should change your Class name.
最后,我尝试将我的类名从HashMap更改为HashMapDemo。现在,我只想告诉您,您的类名可能是 java.util.* 之一,您应该更改您的类名。
回答by sergiotarxz
If you have named your class with the same name as other class you can always do the following:
如果您已将您的班级命名为与其他班级相同的名称,您始终可以执行以下操作:
Map<String, Integer> cache = new java.util.HashMap<String, Integer>();
It's a common mistake when you are trying a new language/library capability to call the class with the same name as the class you are trying, anyway this is not only useful to this purpose.
当您尝试使用新语言/库功能调用与您正在尝试的类同名的类时,这是一个常见的错误,无论如何这不仅对这个目的有用。
For example there are multiple classes name Datelike Java.sql.Dateand java.util.Date, in this case if you need to reference both it's useful to know that.
例如,有多个类名称Date ,如Java.sql.Date和java.util.Date,在这种情况下,如果您需要引用两者,了解这一点很有用。