Manish Kumar
Manish Kumar

Reputation: 589

HashMap is returning NULL

I have a Class Levels which has a hashMap declared.

public class Levels{
   private final Map<Unit, Object1> rateUnitCost;

   public Levels(Map<Unit, Object1> levels) {
  this.rateUnitCost = new HashMap<Unit, Object1>(levels);
 }

  public Object1 getCoverageLevel(Unit unit, Phase aP) {

        return rateUnitCost.get(unit);
        }
 }

I am calling getCoverageLevel() method from other class and i am instantiating the Levels class rateUnitCost property as well from another class.

When seeing in debugger i am finding this value for rateUnitCost and unit object. rateUnitCost: - Hash Map Values

  rateUnitCost  HashMap<K,V>  (id=1248) 
     [0]    HashMap$Node<K,V>  (id=1266)    
   key >Unit  (id=1249) 
        amount  Money  (id=1267)    
        flags   ArrayList<E>  (id=1268) 
        procedureId 7156    
        ParticipationId 104152413   
  value >Object1  (id=1250) 

Now value of unit object is below :-

unit    Unit  (id=1251) 
amount  Money  (id=1258)    
flags   ArrayList<E>  (id=1259) 
procedureId 7156    
ParticipationId 104152413   

when i match the value of key with this object then its matching . But at the time of rateUnitCost.get(unit) its returning null even though Object1 is set. Object1 is getting returned from other class using below line: -

return new Object1();

Can anyone please help me to resolve this mystery.?

BasicUnit is a class which is implementing the Unit interface. BasicUnit have equals method as below :-

public boolean equals(Object o) {
  if (this == o) return true;
  if (o == null || getClass() != o.getClass()) return false;

  BasicUnit basicUnit = (BasicUnit) o;

  if (flags != basicUnit.flags) return false;
  if (procedureId != basicUnit.procedureId) return false;
  if (ParticipationId != basicUnit.ParticipationId) return false;
  if (amount != null ? !amount.equals(basicUnit.amount) : basicUnit.amount != null) return false;

  return true;

}

and HashCode :-

 public int hashCode() {
  int result = procedureId;
  result = 31 * result + ParticipationId;
  result = 31 * result + (amount != null ? amount.hashCode() : 0);
  result = 31 * result + (flags == null ? null : flags.hashCode());
  return result;

}

Upvotes: 0

Views: 110

Answers (1)

dcsohl
dcsohl

Reputation: 7396

if (flags != basicUnit.flags) return false;

You are checking for whether your Unit objects have exactly the same ArrayList of flags. This is not an equals()-type equality check; this is checking for literally the same ArrayList of flags. Now, you haven't provided the constructor etc, but I highly doubt you are reusing the same ArrayList.

Check for !(flags.equals(basicUnit.flags)) instead. Do note that ArrayList.equals() uses the E.equals() implementation, so be sure that that is implemented.

Also, note that ArrayList.equals() checks for the same list entries in the same order. I don't know if the order of your flags matters but I suspect it probably does not. You might consider making your flag collection a Set if this is the case.

Upvotes: 2

Related Questions