• 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

Retrieving a method with a hashmap

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have a java program where one user will send a request to another user and when that user accepts the request the location of the person that accepted and the time that they accepted are stored in a hashmap. I decided that the best way to store the location and time was in a duel object so this is the code I have so far



And then the hashmap is



and then when I go to put something into the hashmap


.
Location being the name of the class

Now when a user sends a request to the other person and the person accepts it, the values are passed to the 'locationTime' method but then my question would be how would I iterate through the hashmap so I could get it to say something along the lines of "<User 1> and <User 2> accepted each others request at <loc> at <time>" And then how would I use the 'loc' and 'time' variables from the hashmap? For example I have the name of one of the users and I want to only get the time that the request was accepted).
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ethan Smith wrote:Now when a user sends a request to the other person and the person accepts it, the values are passed to the 'locationTime' method but then my question would be how would I iterate through the hashmap so I could get it to say something along the lines of "<User 1> and <User 2> accepted each others request at <loc> at <time>" And then how would I use the 'loc' and 'time' variables from the hashmap? For example I have the name of one of the users and I want to only get the time that the request was accepted).


Well first off, I wouldn't use Strings, because StringsAreBad (←click).

Set up a class that contains the relevant information (RequstConfirmation?) and use that as your key - perhaps in more than one map.
Also: LinkedHashMap's retain information in insertion order for iteration purposes, which you might find useful.

Another possibility is to structure the information the information the way it appears you want to retrieve it, ie:
HashMap<User, HashMap<User, Method>>

Winston
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, putting a Method in the HashMap and expecting that Method to retain data is not a good idea. The Method object is not like closures that some other languages have. If you call your locationTime() method, then pass a java.lang.reflect.Method instance into the HashMap and expect it to be able to recall the data you passed in to the locationTime() method, you are mistaken.

What you should do is make a small Data Transfer Object (often called a ValueObject) which stores the data and put that object into the HashMap.
 
reply
    Bookmark Topic Watch Topic
  • New Topic