• 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

How to sort Map?

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have


I want to display the Map entries according to the Long values, corresponding
to the String, in the ascending order.

Please suggest, the proper way to do so!

Thanks,
cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry your question is not clear to me. Do you wish to sort based on keys or values?
If it's keys, it's quite simple, just use a TreeMap.

But it it's about sorting by the values, hmm...
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... right, if you want to sort by values, consider a BidiMap implementation in the Apache Collections API:

Commons Collections

Cheers
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Stuart,

What if it is sort by values!

Stuart, you seem to me as fast as electricity. Before I could post, you had an
answer. Please suggest some conversion mechanism, converting values to list
and then sorting, then showing according to keys...

Isn't it?


Thanks,
cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to discuss the implementation of a new algorithm for this (instead of using the lovely apache commons code), you can rest assured I won't be as fast as electricity ..

let's see... can we use something other than a hashmap?

I can think of storing Map.Entry objects (if the HashMap object is already given to you, get the Entry object Set using the appropriate method)
in a List and sorting that list by using a custom Comparator which helps sort by Entry values.

Hope I am clear with the basic idea, but this is just a thought running in my head and I haven't considering all aspects of implementing it, but maybe you want to try some code taking this as a cue and we can discuss it further?
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


you might b looking for somthing like this.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah Saqib,

That's true reply. That is what I was thinking of!

Thanks!


Regards,
cmbhatt
 
saqib sarwar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Pleasur
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code posted by Saqib is not 100% Java5. Can you convert it to complete Java 5 (if you are taking the J5 exam)?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are true Stuart,

Code compiles with warning!
I am trying to convert it to the form that compiles without warning(Java 5).

Thanks for reminding me!



Regards,
cmbhatt
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, let's make any Apache code "out of bounds" for a discussion in the SCJP forum :roll:
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
Also, let's make any Apache code "out of bounds" for a discussion in the SCJP forum :roll:



Why??
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The SCJP 5.0 exam already has a lot of topics! There's a lot of ground to cover, just to learn what's in the Sun objectives (which can be found here: http://www.sun.com/training/catalog/courses/CX-310-055.xml ).

In general, we like to reserve this forum for discussions that fall within the scope of topics that are covered in the exam. In this case, the relevent objective starts out:

"Use capabilities in the java.util package..."

Just trying to help everyone's studies be as efficient as possible

Thanks,

Bert
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Berts,


"Use capabilities in the java.util package..."



That is what my expectations were to have some solution straight from
java.util package. For current purpose (SCJP 5), people like me wont have
any interest to look forward for third party solutions.

My intention was to see how can we do that using java.util package.


Thanks,
cmbhatt
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bert for the explanation. Point taken. In fact, this dawned upon my mind (at least my subconscious mind!) after I asked the question here.

I agree -- and wholeheartedly, that the idea of this forum is to exercise one's own algorithmic skills within the bounds of the set objectives.

I was more in a developer mode than a certificate-aspirant one. I will recalibrate accordingly.

However, I reserve my old stand for just one reason: to those who may be willing to, poring over such pre-written code might give valuable ideas.

Of course, only Javafolk who have adequate experience in development would be amenable to doing this, and it seems to me that there are many here -- without being disparaging, who have less than a long-standing development experience. So, I guess I should mention such other pointers with the appropriate caveat!
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your understanding Stuart! It IS an interesting topic and I think it would be fair if you wanted to spin off an Adobe thread, maybe in the Intermediate forum? I don't think this would qualify as a dreaded "cross-posting" In fact you could drop names if you want to, and if you do start another thread you could add the link to it here for those SCJP candidates who want to do a little "extra credit" studying

Bert
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems to be a good way of doing it. I hope I don't find it too tedious to spawn another thread in an adjacent forum and the forum software we use makes it quite easy (lazy me )

Cheers.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,what about reverse the map then compare it as i see in K&B master exam (:
reply
    Bookmark Topic Watch Topic
  • New Topic