• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sort map by value

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to sort a map by value (instead of by key).
The following is my test program.
LString is a third-party library which I do not have the source code.
Please help to fix the following error:
Bound mismatch: The generic method entriesSortedByValues(Map<K,V>) of type Utils is not applicable for the arguments (Map<String,LString>). The inferred type LString is not a valid substitute for the bounded parameter <V extends Comparable<? super V>> P.java
Type mismatch: cannot convert from SortedSet<Map.Entry<String,LString>> to Map<String,LString> P.java


 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That method doesn't return a Map, so you can't assign its return value to a Map. You can fix it like this:
 
albert kao
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:That method doesn't return a Map, so you can't assign its return value to a Map. You can fix it like this:



How to modify the method so that it will return a sorted map?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a Map does not necessarily have an order - or at least no guarantee of it. To quote the API:

The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not.


My question to you is why does it matter what order they are stored?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

albert kao wrote:How to modify the method so that it will return a sorted map?



What do you need a Map for? You have a Set of Map.Entry objects; what can you do with the Map that you can't do with that Set?

I'm assuming that you need the entries sorted by value because you're then going to iterate through the entries and display them in that sequence. You can iterate through the entries of the Set you have and do that perfectly well. There's no need to make a Map out of them.
 
Hoo hoo hoo! Looks like we got a live one! Here, wave this tiny ad at it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic