I’m trying to create a Hashmap to perform a lookup for me. However when I run this test code, the output is null. I think it has to due with the nature of how the keys are being stored, but Im not positive. Maybe it’s a similar quirk like how the var1 == var2 are not equals unless they point to the same Object in memory, instead you have to use the var1.equals(var2)?
There are two classes to test this.
TestCard.java
import java.util.HashMap;
public class TestCard {
// HashMap for SpecialK Lookup
private static HashMap<Card, Integer> specialKLookup = new HashMap<Card, Integer>();
// Constructor
public TestCard(){
}
public static void main(String[] args) {
Card[] cards = new Card[3];
cards[0] = new Card((short)12, (short)0);
cards[1] = new Card((short)0, (short)1);
cards[2] = new Card((short)5, (short)2);
/* Build SpecialK Lookup HashMap.
* Ace of Spades = 0
* Ace of Hearts = 1
* Ace of Diamonds = 2
* Ace of Clubs = 3
* ...
* Two of Clubs = 51
*/
Integer specialKCounter = 0;
for(int i=12;i>=0;i--){
for (int j=0;j<4;j++){
specialKLookup.put(new Card((short)i, (short)j), specialKCounter++);
}
}
System.out.println(specialKLookup.get(cards[0]));
}
}
Card.java
public class Card{
private short rank, suit;
private static String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"};
private static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
//Constructor
public Card(short rank, short suit){
this.rank = rank;
this.suit = suit;
}
// Getter and Setters
public short getSuit(){
return suit;
}
public short getRank(){
return rank;
}
protected void setSuit(short suit){
this.suit = suit;
}
protected void setRank(short rank){
this.rank = rank;
}
}
The class (
Card) is missing a proper implementation ofequals(Object)andhashCode()Without both of these defined it will just not work. (It compiles fine because these methods are both virtual and inherited in all objects as they are part of Object: HashMap can’t enforce this at compile-time.) See the links above for the contract that is required.
Both of these methods are required to be implemented because
hashCodedetermines the hash-bucket used in the HashMap implementation andequalsis to ensure that an object is value-equals (multiple objects can have the samehashCode, which is whyequalsis also required). See Hash table for more general hash details.If these methods are not overloaded then the implementations defined in Object are used. That is,
x.equals(y)has near-x == ysemantics andhashCodereturns a stable number per contract. This effectively makes the map work like an identity map (when Card objects are keys): only the exact same object can retrieve a previously stored value — every other get will return null, as observed.Happy coding.