• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving a method with a hashmap  RSS feed

 
Ethan Smith
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!