Maintain sorting by value

As you know, SortedMap supports records sorted by key. I read a lot of threads on this forum and saw a lot of examples that sort SortedMap by values. However, as you know, when I placed the default SortedMap element, it does not sort the map again, just put this new entry where it should be.

For instance,

SortedMap<String,Person> sortedMap = new TreeMap(); Person p1 = new Person("John",38); sortedMap.put(p1.getName(), p1); Person p2 = new Person("Tom",34); sortedMap.put(p2.getName(), p2); // does not sort, maintains sorted set by comparing the other values Person p3 = new Person("Susan",21); sortedMap.put(p3.getName(), p3); // does not sort, maintains sorted set by comparing the other values 

In many threads on this forum, I saw a lot of code that sorts SortedMap by value, by calling a sort method like:

 sortedMap.sort(sortedMap.entries()); 

This or something else method is called to get the values ​​as sorted.

But I need a Map implementation that saves the values ​​as sorted without the call sorting method, as I explained above. For example, in the above code, I can simply call the firstKey () method; but instead, I need to call the firstValue () method.

 Person minimumAgePerson = sortedMap.firstValue(). System.out.println(minimumAgePerson.getName()); // it should print "Susan" 

SortedSet is not suitable for my applications, because I can put some new objects (Person) whose key values ​​are already on the map, these only added records should override existing objects (so I need a map):

 Person p4 = new Person("Susan",39); sortedMap.put(p4.getName(),p4); Person newMinimumAgePerson = sortedMap.firstValue(); System.out.println(newMinimumAgePerson.getName()); // it should print "Tom" 

Is there an implementation to execute these taks or do I need to implement SortedSet myself?

+4
source share
3 answers

I think the best way for you is to create a custom type containing both Map (for key association) and SortedSet (for sorting values)

It’s not clear to me if you want to have the same value for two different keys. In this case, you will need to use some sort of SortedMultiSet.

+3
source

Often the easiest and safest way to solve this problem is to write a class that uses two different standard collections. The class can offer exactly those methods that you need, not necessarily corresponding to any java.util interface.

Given these requirements, I would use SortedMap to store values ​​in combination with HashMap mapping keys to values. To prevent duplicate keys, put a key-value pair in the HashMap, checking the result of put. If the key was already present, delete the old value from SortedMap before adding the new value.

If you have additional requirements, this particular design may not cover everything, but the concept of combining java.util structures is generally useful.

+3
source

There is no implementation that contains sorting by both key and value. But in fact, any implementation that did this will in any case require a separate data structure to sort by value, so you can also create this data structure.

I would suggest either simply implementing a sorted structure using an existing one like TreeMap or PriorityQueue, depending on your needs. Once this was done, I probably expanded TreeMap with a custom structure and redefined the put, remove, addAll, etc. methods to put them both in the supermap and in your sorting by key structure.

+1
source

Source: https://habr.com/ru/post/1480736/


All Articles