getting Employee Object out of HashMap


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: getting Employee Object out of HashMap

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Location
    Madison, Wisconsin
    Posts
    6

    getting Employee Object out of HashMap

    What is I want to do is retrieve a value out of a hashmap, the value is an Employee Object stored in an ArrayList.

    ? How do I accomplish this?

    =========== here's my psuedocode ========

    read employee.file into an strLineArray
    split the strLineArray into ID, FirstName, LastName, and Phone fields;

    Employee employee = new Employee(ID, FirstName, LastName, Phone);

    ArrayList directory = new ArrayList();

    directory.add(employee);

    lastNameMap = new HashMap();

    lastNameMap.put(new String(lastName), directory);
    ===================================

    Question: how do I get an employee out the lastNameMap, given a lastName information?

    Can someone also verify that I .put the directory (ArrayList) in the lastNameMap?

    Thanks a bunch,
    Fischen

  2. #2
    Join Date
    Aug 2003
    Posts
    313
    I think you want to do .put(lastName, employee). Then you should be able to do .get(lastName) and it will give you employee back.

    Hope this helps.
    ~evlich

  3. #3
    Join Date
    Dec 2005
    Location
    Madison, Wisconsin
    Posts
    6
    For a different method, for add and retrieving ids from an idMap; this works fine. What about people with the same Last Name. For example if you have 14 "smiths" I would only receive the last "smiths" in the hastable.

    Fischen

  4. #4
    Join Date
    Aug 2003
    Posts
    313
    Ok, in this case, you will want to preserve the information. You can do this very easily using a modified put.
    Code:
    void putSet(String lastName, Employee emp, HashMap map) {
      Set set = map.get(lastName);
      if( set == null ) {
        set = new TreeSet();
        set.add(emp);
        map.put(set);
      } else {
        set.add(emp);
      }
    }
    I personally think that sets are a little more logical here than array lists because you can get an implicit ordering and you don't want a person to occur multiple times in your table.

    Hope this helps.
    ~evlich

  5. #5
    Join Date
    Dec 2005
    Location
    Madison, Wisconsin
    Posts
    6

    thanks evlich

    I'll give your method a try.

    Fischen.

  6. #6
    Join Date
    Dec 2005
    Location
    Madison, Wisconsin
    Posts
    6

    found workable solution

    I wrote a separate method to add objects to a hashtable. In this situation, I need to be able to store multiple last name.

    -----method accepts two arguements(String lastName, Employee aEmployee)
    if (lastNameMap.containsKey(lastname) {
    ArrayList uniqueLastName = new ArrayList();
    uniqueLastName.add(aEmployee);
    lastNameMap.put(lastName, uniqueLastName )
    }
    else {
    ArrayList sameLastName = new ArrayList();
    sameLastName = (ArrayList)lastNameMap.get(lastName);
    sameLastName.add(aEmployee);
    lastNameMap.put(lastName, sameLastName)
    }

    --------------------
    The above method allows you to add multiple Employees of 'Smith' with a key of 'Smith'

    Without doing this (unless you use a database) you will not be able to add say, 'Jackie Smith' and 'John Smith' because the last add to the lastName map will overwite the previous. Because the key in a hashtable must be unit. So a lastNameMap can only contain one 'Smith' key.

    I hope this helps anyone that reads this posting.

    Fishen

  7. #7
    Join Date
    Dec 2005
    Posts
    10
    Hi all,

    I am new to this forum. Can you guyz please give some ideas to improve this website in terms of contents and look n feel.
    http://javainterviewquestions.50webs.com/

    Regards
    Vivek

  8. #8
    Join Date
    Dec 2005
    Posts
    10
    http://javainterviewquestions.50webs.com/
    I have compiled all possible questions. Feel free to suggest your own q/a.

Similar Threads

  1. Replies: 0
    Last Post: 08-08-2002, 06:56 AM
  2. Even C++ had this much right...
    By Derek Mooney in forum .NET
    Replies: 94
    Last Post: 10-29-2001, 09:44 PM
  3. Validating XML
    By Jaco de Villiers in forum XML
    Replies: 1
    Last Post: 06-01-2001, 06:50 PM
  4. Replies: 1
    Last Post: 10-24-2000, 05:48 PM
  5. Re: COM object Password Security
    By Tom Shreve in forum Enterprise
    Replies: 0
    Last Post: 04-07-2000, 09:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center