2

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].

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
Marc
  • 1,094
  • 3
  • 17
  • 38
  • Can you swap the key/value? I.e. is a map 4=>3, 2=>2, 3=>1 acceptable? If so, you should be able to use a SortedMap. – aioobe Dec 12 '13 at 11:15
  • *I tried to sort the map which worked, then my output was this:* - If this worked(I don't know how you did this), then you can just get the `kepSet()` for sorting the `HashMap` and convert it to an `Array`(if you want the result as an array). – Rahul Dec 12 '13 at 11:20
  • possible duplicate [how-to-sort-a-mapkey-value-on-the-values-in-java](http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java) – vels4j Dec 12 '13 at 11:23
  • here is the answer that before asked here ; http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java – newuserua Dec 12 '13 at 11:26

4 Answers4

1

LinkedHashMap preserves the order of insertion.

Output:

Key : 3 Value : 4
Key : 1 Value : 3
Key : 2 Value : 2

Code:

import java.util.*;

class SortMapValues {
    public static boolean ASC = true;
    public static boolean DESC = false;

    public static void main(String[] args) {
        // Creating dummy unsorted map
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        map.put(1, 3);
        map.put(2, 2);
        map.put(3, 4);

        Map<Integer, Integer> sortedMapDesc = sortByComparator(map, DESC);
        printMap(sortedMapDesc);

    }

    private static Map<Integer, Integer> sortByComparator(Map<Integer, Integer> map, final boolean order) {
        List<Entry<Integer, Integer>> list = new LinkedList<Entry<Integer, Integer>>(map.entrySet());

        // Sorting the list based on values
        Collections.sort(list, new Comparator<Entry<Integer, Integer>>() {
            public int compare(Entry<Integer, Integer> o1,
                    Entry<Integer, Integer> o2) {
                if (order) {
                    return o1.getValue().compareTo(o2.getValue());
                }
                else {
                    return o2.getValue().compareTo(o1.getValue());
                }
            }
        });

        // Maintaining insertion order with the help of LinkedList
        Map<Integer, Integer> sortedMap = new LinkedHashMap<Integer, Integer>();
        for (Entry<Integer, Integer> entry : list) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }

    public static void printMap(Map<Integer, Integer> map) {
        for (Entry<Integer, Integer> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() + " Value : "+ entry.getValue());
        }
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
1

You could do something like this:

int max = Integer.MIN_VALUE;
int maxKey;

for (Map<Integer, Integer> entry: map.entrySet()){
    if (entry.getValue() > max){
        maxKey = entry.getKey();
        max = entry.getValue();
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

Use a TreeMap not a HashMap, that will keep them sorted.

Then your desired result just becomes hm.values();

If you want to sort by the value then just put them in the map the other way around and map score to course id - then use the TreeMap as above. If you have "draws" then you may need to do a map of score to a list of course ids though and then decide how to handle that.

Tim B
  • 40,716
  • 16
  • 83
  • 128
  • This sorts on keys, not on values. The question indicates that he's after the latter. – aioobe Dec 12 '13 at 11:17
  • Regarding "just put them the other way around"... This will of course just work if he really doesn't need efficient key to value lookup. – aioobe Dec 12 '13 at 11:19
  • I used this, but the output is the wrong way around now. At the moment the output is: [2,1,3] and as I said above I need [3,1,2]. But the TreeMap does work. Do you know how to fix this? – Marc Dec 12 '13 at 11:21
0

There is no order guaranteed in HashMap. If you want to focused on order you should use LinkedHashMap .

If you are not focusing on insertion order better to use TreeMap since it is a sorted.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115