1

I have a Map<String,Map<Integer,String>>

Sample data in this map

("aa" , ((3, "xx"),(5, "yy"),(1,"zz")))

("bb" , (5, "zz"))

Here Integer key of the inner map lies between 1 to 5. It is basically a priority number.

Now I need to fetch value for some key (say aa). It should return values from the inner map with the highest priority number (key).

In above example , yy should be returned.

Note: order of insertion of data in map has nothing to do with order of inner map's key.

What should I do -

  • Use sorted inner map on the basis of key while populating map data?
  • Iterate over the map with the highest priority value (5 in this case) to lowest (1 in this case)?
  • Sort inner map in ascending order of key and get the last value?
5
  • Won't the second aa override the first value? I guess it should be Map<String,List<Map<Integer,String>>> Commented Sep 20, 2016 at 17:13
  • @DeendayalGarg I think the second aa was just referring to adding another value to the inner map. Commented Sep 20, 2016 at 17:18
  • @DeendayalGarg i updated question Commented Sep 20, 2016 at 17:20
  • @4castle. Right. wasn't clear form the question. thanks Commented Sep 20, 2016 at 17:20
  • @4castle you are right. Commented Sep 20, 2016 at 17:21

3 Answers 3

2

Options 2 & 3 are less efficient, because you will have to sort/iterate every time you poll for a value.

You can implement option #1 with a TreeMap and all the sorting will be handled for you as elements are added. Then use TreeMap#lastEntry() to get the entry with the highest key value.

Using some Java 8 features:

Map<String,TreeMap<Integer,String>> outerMap = new HashMap<>();

public void insert(String outerKey, Integer innerKey, String innerValue) {
    outerMap.computeIfAbsent(outerKey, k -> new TreeMap<>())
        .put(innerKey, innerValue);
}

public String pollHighest(String outerKey) {
    return Optional.of(outerKey)
        .map(outerMap::get)
        .map(TreeMap::lastEntry)
        .map(Map.Entry::getValue)
        .orElse(null);
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for sample code using Java8 But I am using Java 7. Although I got your point.
1

Option #2 will have O(n) complexity in worst case.

Option #3 will need sorting at each and every step. That would be O(n) in worst case, if you insert elements one by one using Insertion sort algorithm.

For internal map, you can use TreeMap. It will keep the data sorted for you. TreeMap guarantees log(n) time complexity for operations. For your case, a priority queue (made using max heap) can also be used.

2 Comments

One thing to note when using a priority queue though, is that it will allow duplicates.
Yes, Priority Queue will allow duplicates. But, I don't think it will affect above requirement. The duplicate values can be eliminated by the logic.
0

If no other operations are done on this map, them it is better to go with option#1 which builds the map as per the requirement. No need of further iterations over it.

I suggest to use treeMap for the innerMap on the basis of key.

1 Comment

Does this add any more than my answer? If you agree with an existing answer, just upvote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.