• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use Map interface to look for a value in a 'struct' like implementation  RSS feed

 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im a beginner in Java, I am trying to implement a struct like data structure in Java using classes and I want to look for a match in that...The code is as follows:



As seen, I have a class operations, in which I have various fields. In another method called execute, I add values into those fields in the form of array. Now I get an input from the user for name, I want to search for it in the class/struct implementation and return the corresponding value for stream to the user. I understand that I have to implement a Map interface, but how do I do it? Im confused. Please help.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can either just search through your list of objects looking at each one in turn until you find the one with the given name or you can put all the objects in a HashMap keyed on the object's name and then you can use the get(..) method to get the object associated with given name. The HashMap approach is certainly worth doing if you are likely to be making a number of lookups and/or there are many objects to search through.
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I now have the code as follows:



Can you tell me if there is any problem with the way Map interface is implemented? Because everytime I print only the last element in the array for name/stream/funct/source rather than the match for the string X that I am looking for!! Please tell me if you know what the problem is.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest I don't really understand what your execute() method is supposed to do. The method name 'execute' is so vague it doesn't state it the method's intended purpose and the method appears to be doing more than one thing.

What I can tell you is you are adding the whole list keyed on a single value but you need to add each object in the list keyed on it's unique name. Also you have an instance variable called 'map' and a local variable called 'map' which is confusing and bad practice. Note: anything placed into the local 'map' variable will be lost when it goes out of scope ie when the method returns.
BTW I probably should have mentioned this earlier but using a map will only work if each object has a unique name.
 
Preet Rams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tony for the reply! I understood that I was making a mistake by assigning the same key to all the entries in Map. I have changed the program now and it looks like this:



Only question I have now is...how do I print or get the values when I do as in the program? The execute method is supposed to return the values for stream/funct/funcgroup for the match in in name. hope you get me now
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Preet Rams, please BeForthrightWhenCrossPostingToOtherSites
http://stackoverflow.com/questions/17062242/using-map-interface-to-look-for-a-value-in-a-struct-like-implementation-of-cla
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!