I made a map like this:
Map <Integer, Integer> hm = new HashMap<Integer, Integer>();
for(int i = 0; i<courses.size(); i++){
int occurrences = Collections.frequency(subs, courses.get(i).getCourseId());
hm.put(i+1, occurrences);
}
System.out.println("map:");
System.out.println(hm);
The map contains to integers, one which is a course Id and one with the occurrences of how many times the course is in my array. However now the output is:
map:
{1=3, 2=2, 3=4}
I need to get the top values (so I need the second integer value). I tried to sort the map which worked, then my output was this:
{3=4, 1=3, 2=2}
However I need to get the top values and return them to something else. My wanted output is:
3, 1, 2
in an array of course. Thanks for helping in advance!
Edit: I changed the code to this:
Map <Integer, Integer> hm = new TreeMap<Integer, Integer>();
for(int i = 0; i<courses.size(); i++){
int occurrences = Collections.frequency(subs, courses.get(i).getCourseId());
hm.put(occurrences, i+1);
}
and printed the values with:
hm.values();
Then my output was: [2,1,3] I need my output to be [3,1,2].