Why does the Java compiler not like primitive int as type for values in HashMap?

It’s fine with Integer, but not okay with int – Java generics only work with reference types, basically 🙁

Try this – although be aware it will box everything:

HashMap<String,Integer> userName2ind = new HashMap<String,Integer>();
for (int i=0; i<=players.length; i++) {
    userName2ind.put(orderedUserNames[i],i+1);
}

Leave a Comment