It'll work. Here's some code that follows. However, in my sample, I'd rather store 'user' as the key to the HashMap. Also, the code below won't allow for the list to be sorted or anything after it is added to the HashMap.
import java.util.*;
public class VectorAndHashMap
{
static class Preference
{
String color;
String fontSize;
public Preference(String _color, String _fontSize) {
color=_color;
fontSize=_fontSize;
}
public String toString() {
return color+"/"+fontSize;
}
}
public static void main(String[] args)
{
Object[] users = {"smith", "brown", "jones"};
// create the Vector
Vector v = new Vector(Arrays.asList(users));
Object[] prefs = { new Preference("blue", "big"),// smith's
new Preference("blue", "small"),// brown's
new Preference("red", "medium") }; // jones's
// create the HashMap
HashMap hashmap = new HashMap();
// load the HashMap
for(int i=0; i<v.size(); i++)
hashmap.put(new Integer(i), prefs[i]);
// lookup index for "brown"
int j=0;
boolean found = false;
for(j=0; j><v.size(); j++) {
String user = (String)v.get(j);
if(user.equals("brown")) {
found = true;
break;
}
}
// retrieve record
if(found) {
Preference pref = (Preference)hashmap.get(new Integer(j));
System.out.println("brown's record: " + pref);
}
}
}
>
> Thanks for your reply! And it is helpful for me.
> However, when the vector is full and it changed it's
> size, will the hashmap still has correct key? Since I
> don't know whether the vector size change will casue
> what result to the hashmap.
I'm not sure if it will work or not (I suspect not) since it was pointed out that you can't sort the Vector afterwards.
You also need to realize that this isn't very efficient since the hash routine will need to use the object to generate its actual key value. If you really need to do this, I would recommend that you derive from Vector and provide a method to generate a key value. You do this by implementing your own public int hashCode()
method, overriding the inherited one (from Object). This should allow you to modify the Vector in any way you want, so long as the returned hashCode is in no way dependent on the contents of the Vector itself.
I use hashmap to store data between the vector of User object in DB, and the vector of User name in list. So I need to do a mapping between list and the object in DB, in order to perform a del function, which can erase the user in the list and DB at same time. The list is only showing the name of users. But it is not the primary key of DB, so it is diffcult for me to del it in DB by just choosing the name in the list.
> I use hashmap to store data between the vector of User
> object in DB, and the vector of User name in list. So
> I need to do a mapping between list and the object in
> DB, in order to perform a del function, which can
> erase the user in the list and DB at same time. The
> list is only showing the name of users. But it is not
> the primary key of DB, so it is diffcult for me to del
> it in DB by just choosing the name in the list.
Oh, I think I misunderstood what you were asking!
Rather than using the position of the user name in the list of names, why not use the actual name? You might lose a little performance, but you gain a lot in the way of robustness, flexibility, and maintainability.
Consider this oversimplified example:
HashMap userDataMap = new HashMap();
Vector userObjects = getAllUserObjects(); // from db
Iterator iter = userObjects.iterator();
UserObject user;
while( iter.hasNext() )
{
user=(UserObject)iter.next();
userDataMap.put( user.getName(), user );
}
Vector userNames = new Vector( userDataMap.keySet() );
java.util.Collections.sort( userNames ); // sort usernames!
listWidget = new JList( userNames );
...
private void deleteSelected()
{
Object selected =listWidget.getSelectedValue();
dbDelete( (UserObject)userDataMap.get(selected) );
userDataMap.remove( listWidget.getSelectedValue );
}
This way the dbDelete method has access to all the information about the user. You can use the map keyset as the values you show on your list, and they'll always stay in sync, whether you reorder the objects or not.