Why can't I initialize the map <int, String>?

I want to save a set of int / String values, but int not necessarily incremental, i.e. data can be:

 <1, "first">, <3, "second">, <9, "third">. 

So, I am trying to create the equivalent of the C # Dictionary<int, string> , but it just does not compile, saying "Syntax error on the token" int "," Sizes expected after this token "on the line:

 private Map<int, String> courses; 

Can someone tell me why this is? And a good alternative to creating an object as a placeholder for int and String , and then using an array to store them?

+6
source share
1 answer

You cannot use primitive types as general type arguments.

Use

 private Map<Integer, String> courses; 

See additional restrictions on Generics here .

Contribution Dev: JLS indicates that only reference types (and wildcards) can be used as arguments of a general type.

+22
source

Source: https://habr.com/ru/post/953593/


All Articles