String类:
public boolean equals(Object anObject) 重载了object的equals方法
-
Compares this
string to the specified object. The result is
true
if and only if the argument is notnull
and is aString
object that represents the same sequence of characters as this object.
public int hashCode() 同样重载了object的hashCode方法
-
Returns a hash code for this
string. The hash code for a
String
object is computed ass[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
int
arithmetic, wheres[i]
is the ith character of the string,n
is the length of the string, and^
indicates exponentiation. (The hash value of the empty string is zero.)
但是StringBuffer并没有重载equals和hashCode方法,所以当调用这两个方法的时候,还是用的object类中的方法,故而即使两个StringBuffer的内容一样,也可能算出的hashCode并不一样
File类:
compareTo
public int compareTo(File pathname)
- Compares two abstract pathnames lexicographically. The ordering defined by this method depends upon the underlying system. On UNIX systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.
equals
public boolean equals(Object obj) 重载了object的方法
-
Tests this abstract pathname for equality with the given object. Returns
true
if and only if the argument is notnull
and is an abstract pathname that denotes the same file or directory as this abstract pathname. Whether or not two abstract pathnames are equal depends upon the underlying system. On UNIX systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.
hashCode
public int hashCode() 重载了object的方法
-
Computes a hash code for this abstract pathname. Because equality of abstract pathnames is inherently system-dependent, so is the computation of their hash codes. On UNIX systems, the hash code of an abstract pathname is equal to the exclusive
or of the hash code of its pathname string and the decimal value
1234321
. On Microsoft Windows systems, the hash code is equal to the exclusive or of the hash code of its pathname string converted to lower case and the decimal value1234321
. Locale is not taken into account on lowercasing the pathname string.
所以给一个文件和其copy分别建立一个对象,可能这两个对象的哈希值并不相同,因为和路径有关,并不意味着这两个文件不一样(这儿是指文件内容),如果用hash软件或者md5软件来计算这两个文件的hash值,就应该是一样的