int的哈希码
什么是基本类型的哈希码,例如int?
例如,让我们说num是一个整数。
int hasCode = 0; if (num != 0) { hasCode = hasCode + num.hashCode(); }
对于int
的hashCode
,最自然的选择是使用int
本身。 一个更好的问题是什么用于long
的hashCode
,因为它不适合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
- ConcurrentHashMap构造函数参数?
- Apache Commons Lang3 Hashcode,Equals和ToString包括Enums
- 使用String hashCode()方法?
- 为什么默认的Object.toString()返回hashCode的hex表示?
- 为什么在Object中定义了equals和hashCode?
- 如何在Objective-C中获得Java String.hashCode()的相同结果?
- HashMap优化的影响,它将与每个条目关联的哈希代码缓存到其get方法中
- Joshua Bloch在有效的java中建议如何在Java中使用缓存哈希码?
- 这些实体的equals和hashCode(Spring MVC + Hibernate)