• 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

Problems w/ a HashMap

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Miklos Szeles
can you provide the code for mentioned above saying???
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pankya Jain
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok no problem. do you have idea who provide me java code for my problem???
 
Hold that thought. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic