What's wrong with this? Specifically, what's wrong with intCount.put(i, intCount.get(i)++)
?
public static Map<Integer, Integer> countNumbers(List<Integer> list) {
Map<Integer, Integer> intCount = new TreeMap<>();
for (Integer i : list) {
if (intCount.get(i) == null) {
intCount.put(i, 1);
} else {
intCount.put(i, ++intCount.get(i));
}
}
return intCount;
}
This works, on the other hand
public static Map<Integer, Integer> countNumbers(List<Integer> list) {
Map<Integer, Integer> intCount = new TreeMap<>();
for (Integer i : list) {
if (intCount.get(i) == null) {
intCount.put(i, 1);
} else {
intCount.put(i, intCount.get(i) + 1);
}
}
return intCount;
}
Does it mean I can't increment Integer
s, only int
primitives? The problem is when I cast Integer
into its respective primitive (or rather, an Integer
returning method into its respective primitive) like this
intCount.put(i, ++(int)(intCount.get(i)));
it doesn't work either! Why?
Main.java:30: error: unexpected type
intCount.put(i, ++(int)(intCount.get(i)));
^ required: variable
found: value
1 error