The type HashMap is not generic; it cannot be parameterized with arguments <>Java(16777740)
时间: 2024-05-26 08:11:27 浏览: 18
这问题看起来像是一个编程问题。HashMap 是 Java 中的一个类,它可以存储键值对,并且可以根据键获取值。在 Java5 中,HashMap 可以用泛型进行参数化,但在 Java5 之前,不支持泛型,需要手动进行类型转换。如果你遇到了类似于 "The type HashMap is not generic; it cannot be parameterized with arguments" 这样的错误,请检查你的 Java 版本,或者将 HashMap 的旧型参数化方法应用于你的代码。
相关问题
Cannot infer type arguments for HashMap<>
`HashMap<>` is a generic class in Java, which means that it requires type arguments to be specified. You need to specify the type of keys and values. For example, if you want a `HashMap` that maps strings to integers, you would declare it as follows:
```java
HashMap<String, Integer> map = new HashMap<String, Integer>();
```
In this case, `String` is the type of keys, and `Integer` is the type of values. If you want to use type inference, you can omit the type arguments on the right-hand side of the assignment, and Java will infer them from the context:
```java
HashMap<String, Integer> map = new HashMap<>();
```
Here, Java infers the type arguments based on the left-hand side of the assignment.
Cannot infer type arguments for HashMap<>是为什么
这个错误通常发生在你没有指定 HashMap 的键和值类型时。HashMap 是一个泛型类,需要在创建实例时指定键和值的类型。例如,如果你想要创建一个键为 String 类型,值为 Integer 类型的 HashMap,应该这样写:
```
HashMap<String, Integer> map = new HashMap<>();
```
如果你没有指定键和值的类型,例如下面的代码:
```
HashMap map = new HashMap();
```
那么编译器就无法推断出 HashMap 的类型参数,从而导致"Cannot infer type arguments for HashMap<>"的错误。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)