为什么我的HashMap.get即使在输入正确的键和值后也返回空值? [英] Why does my HashMap.get returns null value even after putting proper key,value?

查看:157
本文介绍了为什么我的HashMap.get即使在输入正确的键和值后也返回空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从哈希映射中检索一些值,然后返回值,我检查密钥是否存在于映射中,并且此检查总是失败,从而导致null值。我已经重写了hash Code和equals方法。有人可以告诉我我在做什么错吗?



类字段:

  private static final List< String> DZ = new ArrayList< String>(); 
private static final Map< Participant,List< String>> subDz = new HashMap< Participant,List< String>>();

我正在放入地图的方法:

  public static synchronized void handleSubs(String [] subData,String dz){
int [] lowdims = new int [subData.length];
int [] highdims = new int [subData.length];
尝试{
for(int i = 1; i< subData.length; i ++){
if(!subData [i] .equals()&&!subData [ i).equals(\ n)){
if(i%2 == 0){
highdims [i] = Integer.parseInt(subData [i] .trim());
}
else {
lowdims [i] = Integer.parseInt(subData [i] .trim()); $!


$ b if(!DZ.isEmpty()){
DZ.clear();
}
DZ.add(dz);
allSubDZs.add(dz);
int [] newlow = removeZeroes(lowdims);
int [] newhigh = removeZeroes(highdims);
allSubs.add(new Participant(newlow,newhigh));
subDz.put(new Participant(newlow,newhigh),DZ);
}

检索值的方法:

  public static List< String> getSubDz(Participant sub){
if(subDz.containsKey(sub)){
return subDz.get(sub);
}
else {
logger.info(订阅DZ不可用);
返回null;






$ getSubDz中的if检查总是失败,我把钥匙放在里面。


hashCode和equals方法:

  @Override 
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result +((DZ == null)?0:DZ.hashCode());
返回结果;
}
@Override
public boolean equals(final Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;

if(this.getClass()!= obj.getClass()){
return false;
}
final SubscriptionHandler other =(SubscriptionHandler)obj;
if(DZ == null){
if(other.DZ!= null){
return false;
}
} else if(!DZ.equals(other.DZ)){
return false;
}
返回true;


解决方案

在密钥类上需要equals和hashcode。这将是您的案例中的参与者类。


I am trying to retrieve some values from a Hash Map, before returning the value I am checking if the key is present in the map or not, and this check always fails which results in null value. I have overridden hash Code and equals method as well. Could someone tell me what I am doing wrong here?

class fields:

 private static final List<String> DZ=new ArrayList<String>();
 private static final Map<Participant,List<String>> subDz=new HashMap<Participant,List<String>>();

Method where I am putting into the map:

    public static synchronized void handleSubs(String[] subData,String    dz){
    int[] lowdims=new int[subData.length];
    int[] highdims=new int[subData.length];
    try {
        for (int i=1;i<subData.length;i++){
            if (!subData[i].equals("") && !subData[i].equals("\n")){
                if (i%2==0){
                    highdims[i]=Integer.parseInt(subData[i].trim());
                }
                else {
                    lowdims[i]=Integer.parseInt(subData[i].trim());
                }
            }
        }
        if (!DZ.isEmpty()){
            DZ.clear();
        }
        DZ.add(dz);
        allSubDZs.add(dz);
        int[] newlow=removeZeroes(lowdims);
        int[] newhigh=removeZeroes(highdims);
        allSubs.add(new Participant(newlow,newhigh));
        subDz.put(new Participant(newlow,newhigh),DZ );
    }

Method where I am retrieving the values:

   public static List<String> getSubDz(Participant sub){
    if (subDz.containsKey(sub)){
        return subDz.get(sub);
    }
    else{
        logger.info("Subscription DZ not available");
        return null;
    }
}

The if check in the getSubDz always fails, even though I put the key in it.

hashCode and equals methods:

   @Override
   public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((DZ == null) ? 0 : DZ.hashCode());
    return result;
}
   @Override
   public boolean equals(final Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj == null) {
        return false;
    }
    if (this.getClass() != obj.getClass()) {
        return false;
    }
    final SubscriptionHandler other=(SubscriptionHandler)obj;
    if (DZ == null) {
        if (other.DZ != null) {
            return false;
        }
    } else if (!DZ.equals(other.DZ)) {
        return false;
    }
    return true;

解决方案

You need equals and hashcode on the key class. This would be the class Participant in your case.

这篇关于为什么我的HashMap.get即使在输入正确的键和值后也返回空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆