Hey, so I’m using my own datatype for a key in a HashMap. Basically the idea is that you can send either an int or String (or both) to find a value. However, I never find values in my HashMap ???
Here’s what I have for the Key class:
public class Key
{
public final int code;
public final String name;
public Key(int in_code, String in_name)
{
code = in_code;
name = in_name;
}
public boolean equals(Object o)
{
if(o instanceof Key)
{
return code == ((Key) o).code && name.equals(((Key) o).name);
}
else if(o instanceof Integer)
{
return code == ((Integer) o).intValue();
}
else if(o instanceof String)
{
return name.equals(((String) o));
}
return false;
}
public int hashcode()
{
return (code << 16) + name.length();
}
}
Here’s the map:
HashMap map = new HashMap<Key, InputOperation>();
and here is how I would like to use it:
m_map.get(97);
m_map.get("Print_Char");
m_map.get(new Key(97, "Print_Char"));
Edit: added new Key to the third get()…