• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Sort Map By Value (value is list) Java

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a scenario where I want to sort the map by value where the value is a list and I want to sort it by list size.

Eg.


Here the Map
[key = 1, value = list of 2 element]
[key = 2, value = list of 10 element]
[key = 3, value = list of 5 element]
[key = 4, value = list of 7 element]


after operation. My result something like
[key = 2, value = list of 10 element]
[key = 4, value = list of 7 element]
[key = 3, value = list of 5 element]
[key = 1, value = list of 2 element]



Thank you
Salman

 
Saloon Keeper
Posts: 13271
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you can't sort maps by value (or any property of the value).

You can convert the map to a list of entries though, and sort that based on the values of the entries.
 
salman khandu
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:No, you can't sort maps by value (or any property of the value).

You can convert the map to a list of entries though, and sort that based on the values of the entries.




How can I achieve it? I have done with that but it doesn't sort by length. Here my code

 
Greenhorn
Posts: 9
Mac OS X Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know whether we can share link from other sites, If we can't do that, I'm sorry.

You should check out these links: link1 and  link2.

Hope this helps!
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salman khandu wrote:
How can I achieve it? I have done with that but it doesn't sort by length.



And what does that code result in?
Can you show what is output from the sorted List?
 
Marshal
Posts: 74037
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ekrem Candemir wrote:I don't know whether we can share link from other sites . . .

Yes, you can.

salman khandu wrote:How can I achieve it?

Why would you want to do that? If you work out what you are going to do, you may be able to find a more elegant way to achieve it.

[edit]Change some text to link
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is also a nice exercise to turn a Map<K, List<V>> into say a TreeMap<V, List<K>>, assuming K and V are such that it makes sense to do so.
 
Rancher
Posts: 508
15
Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ekrem Candemir wrote:I don't know whether we can share link from other sites, If we can't do that, I'm sorry.

You should check out these links: link1 and  link2.

Hope this helps!



The answer provided at link2's web page returns a LinkedHashMap. The solution uses Java 8's streams API, and it has clear explanation.
 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to sort not on a function of the lists, but on a function of the elements of the lists itself, you could use (à la SO)

A Stream-version is also possible, à la SO, although you need a flatMap in between and it would be no simpler than this.
 
Stephan van Hulst
Saloon Keeper
Posts: 13271
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet, edit your method signature so the following call is legal:
 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Stephan,

Still struggling with this, it simply refuses to become second nature... I'll try your challenge!

Last week I really tried to write a generic method that would work for Collections (Lists and Sets), only to find that I had to use a method that was specific to Lists... it is the most complicated part of Java. Wish they had never invented it.      
 
Stephan van Hulst
Saloon Keeper
Posts: 13271
292
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that's the thing. If you need a certain upper bound, it's fine to impose that upper bound. Wildcards just make sure you don't automatically impose restrictions that you don't need.

And I can promise you your Java adventures would be a lot less fun if they hadn't added type bounds.
 
Ranch Hand
Posts: 447
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another possibility is if it's a GUI, you can make a table, JTable for Swing, and when the user clicks on the top column, it orders them by that value.
 
Stephan van Hulst
Saloon Keeper
Posts: 13271
292
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would you express a multimap as a table? A table is more similar to a List<Map<K, V>> than a Map<K, List<V>>.
 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@OP,

I have here a nice way to get the ordered Map as you asked. I think it is different from the answers given sofar, and it is slightly more general, but not as general as Stephan wants it to be. For that I created a new topic, so as not to pollute this topis tto much.

The idea is still to transform a Map<K, V> into a form of Map<function of V, List<K>>, where you can both specify what function f(V) that may be, and to specify how this f(V) should be ordered. In your case f(V) is the size of V, and the ordering I choose in this example is the inverse of the natural ordering of Integers. Well, here goes:

 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Stephan

As to not pollute this topic, I continued with this topic: challenge
 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mooo!

Your posting was just mentioned in the June 2018 CodeRanch Journal and for that you get a cow.
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic