1

I need preserve order by value. I wrote some piece of code, but it is not really optimal.

I have a class like this,

class MyClass extends SomeOtherClass {

    private Map<Object, Object> resMap= new HashMap<>();

    Object getRes(Object key) {
        return resMap.get(key);
    }

    void putRes(Object key, Object value) {
        resMap.put(key, value);
    }

I want EACH time when I am invoking putRes(Object value) preserve order by value. Something like if value (that I received as an input) > all other values in map then put it first, if the value is equals to some value in map, so put it after it.

Appreciate any help.

upd: I need sort by Value and not by key. upd: I need sort by calling some method getPriority() of value.

liotur
  • 809
  • 2
  • 17
  • 36

1 Answers1

1

You can take a look at the SortedMap:

A Map that further provides a total ordering on its keys. The map is ordered according to the natural ordering of its keys, or by a Comparator typically provided at sorted map creation time.

npinti
  • 51,780
  • 5
  • 72
  • 96