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());
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());
Is there an implementation to execute these taks or do I need to implement SortedSet myself?