0

I have a multimap of Integer and List of String[]. Here I need to get the highest 10 values of the multimap based on the key.

I am trying to implement it in a way like, if the keys and values are as below,

   Key   Value
    3     [0,0],[1,0],[0,1]
    6     [0,1],[1,1],[0,0]
    1     [1,0],[1,1],[0,1],[0,0]
    2     [1,1],[0,0]

Now I have to get the highest 10 values based on the key.

As the first highest key is 6, it has to get all the values of 6 --> ([0,1],[1,1],[0,0])

Next get the remaining values of next highest key 3 which is --> ([0,0],[1,0],[0,1])

Next get the remaining values of next highest key 2 which is --> ([1,1],[0,0])

Next get the remaining values of next highest key 1 which is --> ([1,0],[1,1]).

As I need only 10 values, I have to pick only 2 values from the key 1 because (3 values[from key 6] + 3 values[from key 3] + 2 values [from key 2] + 2 values from key 1) which is a total of 10 values.

Here is my code:

Map<Integer, List<String[]>> outdoorElements = new HashMap<Integer, List<String[]>>();
putObjects(outdoorElements,EvaluationCount,schedules);

private static void putObjects (Map<Integer, List<String[]>> outdoorElements, Integer key, String[] value) {
    List<String[]> myClassList = outdoorElements.get(key);
    if(myClassList == null) {
        myClassList = new ArrayList<String[]>();
        outdoorElements.put(key, myClassList);
    }
    myClassList.add(value);
}

I am trying so hard how to get the values. Really appreciated if anyone could guide me through this.

Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
sindhu jampani
  • 504
  • 3
  • 10
  • 30
  • Is ur issue how to sort the keys? or how to get the first 10 values? Can you use TreeMap? Maintain a count for the values? – ajc Nov 07 '17 at 17:02
  • I am a rookie. So I am stuck how to sort and also get the values @ ajc – sindhu jampani Nov 07 '17 at 17:18
  • If you used a TreeMap, the keys would be sorted. You would just have to call descendingMap to have it sorted in desc mode, and iterate through the values. – JB Nizet Nov 07 '17 at 17:24
  • Look into 2 things. 1) Sort your Map. (https://stackoverflow.com/questions/922528/how-to-sort-map-values-by-key-in-java) and iterate over it. 2) Maintain a count of values you get, once the count is reached, you can break out of code and stop. – ajc Nov 07 '17 at 17:25

1 Answers1

1

Create a tree map and set the comparator to reverse order. Create an empty list Loop through each value in the tree map and add a check that check to see if that list (the empty list you create) size is less then 10, if so add value to the list.

      Map<Integer, List<String>> map = new TreeMap<>(Comparator.reverseOrder());
    map.put(1, Arrays.asList("1", "2", "3"));
    map.put(10, Arrays.asList("4", "5", "6", "7"));
    map.put(5, Arrays.asList("8", "9"));
    map.put(110, Arrays.asList("10", "11", "12", "13", "14", "15", "16"));
    int max = 10;
    List<String> tenHighestValue = new ArrayList<>();
    map.values().forEach(list -> {
        if (tenHighestValue.size() < max) {
            list.forEach(str -> {
                if (tenHighestValue.size() < max) {
                    tenHighestValue.add(str);
                }
            });
        }
    });
    System.out.println(tenHighestValue);

I hope that helps

tmj010
  • 34
  • 3