wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problems w/ a  HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problems w/ a  HashMap" Watch "Problems w/ a  HashMap" New topic
Author

Problems w/ a HashMap

G. Graz
Ranch Hand

Joined: Oct 23, 2006
Posts: 30
The program below takes values entered by the user and stores them into a HashMap . I am specifically having issues with option #3 " Retrieve an Employee" . All my data gets entered into the HashMap correctly , but I run option #3 I will get an incorrect result ? from the output the data displayed is the last input form the user ? When I debug the program the "value" in the map is correct with what was entered.......so if my "values" are correct and I am using the "containsvale" method , why is my result returning the last value entered by the user ? Thank you for any suggestions !

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18764
    
  40

so if my "values" are correct and I am using the "containsvale" method , why is my result returning the last value entered by the user ?


It is not "returning" the last value entered by the user -- you just never assigned the firstname and lastname variables, before printing them. So, basically, it is printing the last value that was assigned, which was probably when a user was added.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
Hi,

Fist of all I would advice to reread the documentation of Map. It stores key, value pairs. You would like to use SSN as a key but you put it as a value into the map.

Here you use firstname and lastname as keys, so whenever the user enters a name which's firstname or lastname is already used, then it'll change the map entry.
This mistake makes the whole program incorrect.
To answer your question:
You got the last entered name as an answer for a retrieve, since you print out the last entered name, you do nothing with the map in userDescision 3:

Jaydeep Vaishnav
Greenhorn

Joined: Sep 08, 2009
Posts: 16
HashMap<Integer,List<String>> would store all the information of an employee (firstname,lastname,department,desig. etc) in a list identified by unique key SSN. I don't know whether just first name and last name needs to be stored but this approach will store values in structured way and retrieving would not be a problem.

in the choice 1 (add Employee) create a list for each SSN and then store it as

List <String>employee=new ArrayList<String>();

employee.add(firstName);
employee.add(lastName);
employee.add(...);

ssnKeys.put(SSN, employee);

This way while retrieving if you try ssnKeys.containsKey(SSN), it should return the whole list. Also maintaining methods(addEmployee(),deleteEmployee() etc.) for each of the options would be a good idea.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I think a better(more object oriented) aproach would be to store employee data in an Emlpoyee object.
I saw that you already have an Employee class but you haven't posted the code. I advice you to creaet a Map<Integer, Employee> to store the employees. Create an employee instance when user creates a new employee, then put it to the map using SSN as the key.
Jaydeep Vaishnav
Greenhorn

Joined: Sep 08, 2009
Posts: 16
Oh yes Miklos, I saw that Employee class is already there so good approach would be hashmap with SSN as key and Employee object as value.
Billy Korando
Greenhorn

Joined: Sep 18, 2009
Posts: 15
You are not really using the HashMap to its full potential right now. By using the containsValue() method to look up values, you are using it as little more than a glorified List. I believe someone else has suggested you should use a employee's ssn as the key and create a employee object that hold the employee's name (and probably ssn) and that that as the value. You should be pretty good then.


Check out my blog on software development: http://www.turnleafdesign.com
Pankya Jain
Greenhorn

Joined: Jun 21, 2012
Posts: 2
Miklos Szeles
can you provide the code for mentioned above saying???
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Hello Pankya Jain, welcome to the Ranch.

Please note that you are responding to a topic from almost three years ago - I'm not sure if Miklos is still here to read your request.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Pankya Jain
Greenhorn

Joined: Jun 21, 2012
Posts: 2
ok no problem. do you have idea who provide me java code for my problem???
 
jQuery in Action, 2nd edition
 
subject: Problems w/ a HashMap