int的哈希码

什么是基本类型的哈希码,例如int?

例如,让我们说num是一个整数。

int hasCode = 0; if (num != 0) { hasCode = hasCode + num.hashCode(); } 

对于inthashCode ,最自然的选择是使用int本身。 一个更好的问题是什么用于longhashCode ,因为它不适合int -sized哈希码。 这个以及所有与hashCode相关的问题的最佳来源是Effective Java 。

取自Integer.class源代码:

 /** * Returns a hash code for this {@code Integer}. * * @return a hash code value for this object, equal to the * primitive {@code int} value represented by this * {@code Integer} object. */ public int hashCode() { return value; } 

其中value是整数的值。

没有可用的基本类型int hashCode()方法。

Integer是Wrapper类类型, hashcode()返回一个int

java.lang.Integer.hashCode()方法返回int原始值的哈希码值,但表示为Integer对象。

 /** * Returns a hash code value for an Integer, * equal to the primitive int value it represents. */ public class IntegerDemo { public static void main(String[] args){ Integer i = new Integer("20"); System.out.println("Value = " + i.hashCode()); } }` 

结果:

值= 20

来源链接: http : //www.tutorialspoint.com/java/lang/integer_hashcode.htm