I want to extract the Integer values of a HashMap to an Integer array, discarding the keys. Java demands the array is of type Object. As a Java beginner, I also failed to find a way to convert "Object" to Integer.
The expected output is sorted array of the values: {3, 2, 1}
.
import java.util.*;
public class test {
public static void main(String [] args) {
Integer[] number = {1, 2, 3, 3, 1, 1};
HashMap<Integer, Integer> dist = new HashMap<>();
Integer count;
for (Integer i : number) {
count = 1;
if (dist.containsKey(i))
count += dist.get(i);
dist.put(i, count);
}
// Program fails below, *Object[] cannot be converted to Integer[]*.
Integer[] distArr = dist.values().toArray(); // Works if I replace Integer with Object
distArr.sort(); // This fails on Object[]
// ...
}
}