java hash equal_Java中equal、hashCode等几个方法

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 not

null and is a

String 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 as

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] using

int arithmetic, where

s[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 not

null 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 value

1234321. Locale is not taken into account on lowercasing the pathname string.

所以给一个文件和其copy分别建立一个对象,可能这两个对象的哈希值并不相同,因为和路径有关,并不意味着这两个文件不一样(这儿是指文件内容),如果用hash软件或者md5软件来计算这两个文件的hash值,就应该是一样的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值