Cannot infer type arguments for HashMap<>
时间: 2024-05-07 19:20:52 浏览: 11
`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.