Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java重写hashCode()获取StackOverflowerError_Java_Overriding_Hashcode_Stack Overflow - Fatal编程技术网

Java重写hashCode()获取StackOverflowerError

Java重写hashCode()获取StackOverflowerError,java,overriding,hashcode,stack-overflow,Java,Overriding,Hashcode,Stack Overflow,所以我并不擅长重写hashCode,我似乎在hashCode方法中进行了一些无限递归 这是我的场景,我有一个类DuplicateCache,它是一个缓存对象,用于检查系统中的重复对象。我有一个表示重复对象的静态内部类副本 DuplicateCache维护一个HashMap以跟踪其所有条目。每个条目由一个重复对象作为键和一个长对象作为值组成 我使用复制对象键执行所有操作,当我在HashMap中运行put方法时,在复制对象的hashCode方法中会出现无限递归 hashCode方法重复调用另一个我必

所以我并不擅长重写hashCode,我似乎在hashCode方法中进行了一些无限递归

这是我的场景,我有一个类DuplicateCache,它是一个缓存对象,用于检查系统中的重复对象。我有一个表示重复对象的静态内部类副本

DuplicateCache维护一个HashMap以跟踪其所有条目。每个条目由一个重复对象作为键和一个长对象作为值组成

我使用复制对象键执行所有操作,当我在HashMap中运行put方法时,在复制对象的hashCode方法中会出现无限递归

hashCode方法重复调用另一个我必须重写的类的hashCode,因此我将在后面包含它

无需进一步说明,以下是我针对有问题的重复类的代码:

public static class Duplicate{
    private String merchId;
    private String custId;
    private MagicPrice price;
    private int status;
    private boolean compareStatus;

// snip methods        

    @Override public boolean equals(Object o){
        cat.debug("In the override equals method of Duplicate"); //DELETEME

        if(o instanceof Duplicate)
            return equals((Duplicate) o);
        else
            return false;
    }

    @Override public int hashCode() {
        return merchId.hashCode() + custId.hashCode() + price.hashCode();
    }


    /*Equals method vital to the HashMap cache operations

    How the compareStatus and status fields change this:
    if both objects have true for compareStatus -> Equals will compare the statuses
    otherwise                                   -> Equals will not compare the statuses

    If we only want to do an in_progress check, we need to compare status.
    On the other hand success checks need to ignore the status.
    */
    public boolean equals(Duplicate d){        
        try{
            if(merchId.equals(d.merchId) && custId.equals(d.custId) && (price.compareTo(d.price)==0)){
                if(this.compareStatus && d.compareStatus && this.status != d.status)
                    return false;

                return true;
            }
        }catch(PriceException pe){
            //Catching from MagicPrice.compareTo object method, return false
            return false;
        }

        return false;
    }        
}
这对复制对象(现在是MagicPrice hashCode方法)执行:

@Override public boolean equals(Object o){
    if(!(o instanceof MagicPrice))
        return false;

    MagicPrice p = (MagicPrice)o;

    if(this.iso4217code.equals(p.iso4217code) && this.value.equals(p.value))
        return true;

    else return false;
}

@Override public int hashCode(){
    return value.hashCode() + this.iso4217code.hashCode();
}
在这个类中,值字段是一个BigDecimal,ISO4217代码是一个字符串。由于它的价值,stackTrace最终在BigDecimal hashCode方法中消亡,但我不相信BigDecimal hashCode方法会被破坏

有人能给我解释一下这个哈希代码重写缺少什么吗?我知道一定是我做错了什么导致了这种行为

以下是我的日志文件中的堆栈跟踪:

java.lang.StackOverflowError
    at java.math.BigDecimal.hashCode(BigDecimal.java:2674)
    at com.moremagic.util.MagicPrice.hashCode(Unknown Source)
    at com.moremagic.core.DuplicateCache2$Duplicate.hashCode(Unknown Source)
    at java.util.HashMap.get(HashMap.java:300)
    at com.moremagic.util.ExpirableHashMap.get(Unknown Source)
    at com.moremagic.core.DuplicateCache2.put(Unknown Source)
    at com.moremagic.core.DuplicateCache2.put(Unknown Source)
    at com.moremagic.core.DuplicateCache2.put(Unknown Source)
    at com.moremagic.core.DuplicateCache2.put(Unknown Source)
    <... and it continues with the put references for a looong time ...>
expire是一种基于时间删除表中旧项的方法 hashtable是HashMap对象


谢谢

发布堆栈跟踪。如果您得到了一个SO异常,那么显然在对象定义的某个地方有一个引用循环。堆栈跟踪应该立即显示在何处。

对于StackOverflower错误,堆栈跟踪在何处结束并不重要,这基本上是随机的,可能与问题完全无关,但之前的重复序列是什么-这应该准确指出问题所在


您的hashCode方法看起来很好,它们不应该导致StackOverflower错误。

在大多数情况下,StackOverflower错误意味着您的执行路径中存在无止境的递归。

请包含堆栈跟踪,或者包含足够的堆栈跟踪,以了解其含义。哦,多么愚蠢,感谢您指出我在该put方法中有一个递归调用。。
public Object get(Object key) {
expire();
return hashtable.get(key);
}