0

I'm looking to combine key lookup with ordering on value.

Something like a TreeMap with the comparator on the value, whilst still allowing lookup by key.

So a TreeMap<String, Integer> where the ordered determined by Integer, but also allowing lookup by String.

So I can pop/poll the first key off (as determined by its associated value), but still lookup by string if needed.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • 1
    Wrap something like a `HashMap` and `List` into a class. The `HaspMap` gives you the "lookup by key" and the `List` can give you the ability to key the values in an ordered list – MadProgrammer Dec 20 '21 at 04:30
  • so what is the question... i mean... you already know the answer as *treeMap with a comparator* – Nitika Bansal Dec 20 '21 at 04:30
  • @Nitika no, a treemap does ordering on the key, not the value – NimChimpsky Dec 20 '21 at 04:34
  • @MadProgrammer yeah ok thanks. This isn't a well used concept then ? I thought guava or something might have it. – NimChimpsky Dec 20 '21 at 04:36
  • @NimChimpsky Think about it - a key based structure is focused on making it easier/faster to access a value via the key value (yes, a ordered map will keep the keys in order, but it doesn't care about the values), it's not concerned with the order or providing serial (linked list) or non serial access (array). So, if it's something important that you need, then you to provide some kind of wrapper to implement it. May some third party library does something similar, but it's likely to simply backed in the way described – MadProgrammer Dec 20 '21 at 04:41
  • But you can create a customComparator, pass it when intitlizating treeMap like `Map map = new TreeMap<>(new CustomComparator());` – Nitika Bansal Dec 20 '21 at 05:12
  • @Nitika I know, but that is for THE KEY, not the value – NimChimpsky Dec 20 '21 at 05:38
  • Does this answer your question? [Sort a Map by values](https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values) – Turo Dec 20 '21 at 12:31

0 Answers0