-
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
-
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
-
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
-
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
-
thanks evlich
I'll give your method a try.
Fischen.
-
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
-
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
-
http://javainterviewquestions.50webs.com/
I have compiled all possible questions. Feel free to suggest your own q/a.
Similar Threads
-
By scottish mike in forum .NET
Replies: 0
Last Post: 08-08-2002, 05:56 AM
-
By Derek Mooney in forum .NET
Replies: 94
Last Post: 10-29-2001, 08:44 PM
-
By Jaco de Villiers in forum XML
Replies: 1
Last Post: 06-01-2001, 05:50 PM
-
By Darta in forum VB Classic
Replies: 1
Last Post: 10-24-2000, 04:48 PM
-
By Tom Shreve in forum Enterprise
Replies: 0
Last Post: 04-07-2000, 08: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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks