• 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
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Retrieving different keys when the values in a hashmap are same???

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, would appreciate if anyone can help me out on this.

I have a series of keys and values that I would like to add into a hashmap.

I am printing out the order using the values, only one key will be selected although I have got 3 keys. I am sure that this is the problem of the same values in my Arraylist. Can anyone suggest a solution for this?

What i need is that if it had taken out the 1st key then it will move on to retrieve the 2nd key and so on even though the values are the same.


keys values
1 0.0
2 0.0
3 0.0


 
Marshal
Posts: 28296
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want the keys to be kept with the values, then don't write code which puts them in separate places and handles them separately. Something more like this:

As you see you still have to write a Comparator which orders Map.Entry<Integer, Double> objects via their Double value, but this is almost trivial.
 
Kriss Tan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul, thanks for replying.

Is it possible for you to briefly explain the codes?

What is Map.Entry? etc.. I am still learning more about hashmap in the process..

Thanks!
 
Paul Clapham
Marshal
Posts: 28296
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kriss Tan wrote:What is Map.Entry?

You'll find its API documentation here. And you'll find the API documentation for the java.util package, which contains all of the collections, here. Finally, you will find the Collections tutorial here.

Those should provide a much better learning tool than me trying to explain some specific code. Good luck!
 
Kriss Tan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Paul,

I had spent some time reading through the API and tutorials but am unable to find a sample for comparing objects in Map.Entity.

Is it possible for you to provide me with some codes relating to comparing Map.Entity objects to work on with.

Thanks.
 
Kriss Tan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

I revisit the codes and tried some sample codes and found out certain things I would like to clarify with you.



Why do we need to create a Set Map.Entry object to store the rank. entry set?

I tried it by just storing it in the arrayList and it still works...Any reason why you particularly store it in a Set?



I changed to the above code written.

Looking forward to your reply.

Thanks!
 
Paul Clapham
Marshal
Posts: 28296
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That line of code is identical to the first two lines of code that I posted. The only difference is that my code assigns the entry set to a variable and then passes that variable to the ArrayList constructor, and yours passes the entry set directly to the ArrayList constructor. The end result is the same in both cases.

I did it that way because many people find it harder to understand complicated one-liners. Especially beginners.
 
I need a new interior decorator. This tiny ad just painted every room in my house purple.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic