为什么String中的equals方法不使用哈希? [英] Why does the equals method in String not use hash?

查看:134
本文介绍了为什么String中的equals方法不使用哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类String中的等于方法的代码是

The code of the equals method in class String is

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = offset;
            int j = anotherString.offset;
            while (n-- != 0) {
                if (v1[i++] != v2[j++])
                    return false;
            }
            return true;
        }
    }
    return false;
}

我有一个问题 - 为什么这个方法不使用hashCode()?

I have a question - why does this method not use hashCode() ?

据我所知,hashCode()可以快速比较两个字符串。

As far as I know, hashCode() can compare two strings rapidly.

更新:我知道,那两个不相等的字符串,可以有相同的哈希值。但两个相等的字符串具有相等的哈希值。所以,通过使用hashCode(),我们可以立即看到两个字符串是不相等的。

UPDATE: I know, that two unequal strings, can have same hashes. But two equal strings have equal hashes. So, by using hashCode(), we can immediately see that two strings are unequal.

我只是认为使用hashCode()可以是一个很好的 中的过滤器等于

I'm simply thinking that using hashCode() can be a good filter in equals.

更新2:这里有一些代码,我们在这里谈论。

UPDATE 2: Here some code, about we are talking here.

这是String方法等于的示例

It is an example how String method equals can look like

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        if (hashCode() == anotherString.hashCode()){
            int n = count;
            if (n == anotherString.count) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = offset;
                int j = anotherString.offset;
                while (n-- != 0) {
                    if (v1[i++] != v2[j++])
                        return false;
                }
                return true;
            }
        }else{
            return false;
        }
    }
    return false;
}


推荐答案

Hashcode可能是第一个 - 圆形检查不平等。然而,它提出了一些权衡。

Hashcode could be a first-round check for inequality. However, it presents some tradeoffs.


  1. 字符串哈希码是懒惰计算的,尽管它们是使用守卫值。如果你要比较长寿命的字符串(即,他们可能已经计算了哈希码),这不是问题。否则,您将无法计算哈希码(可能很昂贵)或在哈希码尚未计算时忽略检查。如果你有很多短命的字符串,你会比你使用它更频繁地忽略支票。

  2. 在现实世界中,大多数字符串的第一个字符串不同几个字符,所以你不会先通过检查哈希码来节省太多。当然还有例外(例如URL),但在真实世界编程中,它们很少发生。

  1. String hashcodes are lazily calculated, although they do use a "guard" value. If you're comparing strings with long lifetimes (ie, they're likely to have had the hashcode computed), this isn't a problem. Otherwise, you're stuck with either computing the hashcode (potentially expensive) or ignoring the check when the hashcode hasn't been computed yet. If you have a lot of short-lived strings, you'll be ignoring the check more often than you'll be using it.
  2. In the real world, most strings differ in their first few characters, so you won't save much by checking hashcode first. There are, of course, exceptions (such as URLs), but again, in real world programming they occur infrequently.

这篇关于为什么String中的equals方法不使用哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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