Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Map Value Comparator, sort while inserting

I would like to insert items into a HashMap, TreeMap or SortedMap (you may suggest some other api) using a value Comparator.

I Have read many posts including this one, most of the posts suggest to re-insert the HashMap into a SortedMap with a value Comparator after all items have been inserted.

I am not interested to re-insert all values again. Isn't there an option or a Map similar data structure which supports the activation of a value Comparator after each insert?

If there is a duplicate issue i would appreciate a link (I have done some search, although I may have missed some)

Again, I am interested in adding a value to some kind of ordered Map so that all items will be ordered by the value and not the key, after each single insert.

The value in the Map entry is actually a complex Object with some getters, and I want to sort only by a specific getter on the value object.

like image 950
Michael Avatar asked Jan 16 '23 05:01

Michael


1 Answers

I think what you need is org.apache.commons.collections.bidimap.TreeBidiMap

Red-Black tree-based implementation of BidiMap where all objects added implement the Comparable interface.

This class guarantees that the map will be in both ascending key order and ascending value order, sorted according to the natural order for the key's and value's classes.

like image 160
Amit Deshpande Avatar answered Jan 25 '23 10:01

Amit Deshpande