hashcode

java:String类hashCode()的实现

你。 提交于 2019-12-03 22:29:16
一个对象只有一个hashcode,多个对象的hashcode可能相同。 源代码位置:java-source/java/lang/String.java,hashCode()方法: public int hashCode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; for (int i = 0; i < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; } 关于这个实现: Why does Java's hashCode() in String use 31 as a multiplier? Consistency of hashCode() on a Java string 来源: oschina 链接: https://my.oschina.net/u/940565/blog/339015

Overriding hashcode and equals method in java?

删除回忆录丶 提交于 2019-12-03 21:22:45
I have the classes below: public class Sample implements java.io.Serializable{ //POJO with two fields and getters/setters private String name; private Integer id; //This POJO does not override equals() and hashCode() } public class B{ private Sample sample; //here i need override hashcode and equals() based on **sample** property. } When i tried overriding equals() and hashCode() in the B class I got the error below in Eclipse. The field type com.mypackage.Sample does not implement hashCode() and equals() - The resulting code may not work correctly. Now how can I compare two B instances

JavaSE语法

谁说胖子不能爱 提交于 2019-12-03 21:01:16
二、JavaSE语法(上)   1、Java 有没有 goto 语句?     goto 是 Java 中的保留字,在目前版本的 Java 中没有使用。根据 James Gosling(Java 之父)编写的《The Java Programming Language》一书的附录中给出了一个 Java 关键字列表,其中有 goto 和 const,但是这两个是目前 无法使用的关键字,因此有些地方将其称之为保留字,其实保留字这个词应该有更广泛的意义,因为熟悉 C 语言的程 序员都知道,在系统类库中使用过的有特殊意义的单词或单词的组合都被视为保留字。   2、&和&&的区别     &运算符有两种用法:(1)按位与;(2)逻辑与     &&运算符是短路与运算。逻辑跟短路与的差别是非常巨大的,虽然二者都要求运算符左右两端的布尔值都是true整个表达式的值才是true。     &&之所以称为短路运算是因为,如果&&左边的表达式的值是 false,右边的表达式会被直接短路掉,不会进行 运算。很多时候我们可能都需要用&&而不是&,例如在验证用户登录时判定用户名不是 null 而且不是空字符串,应 当写为 username != null &&!username.equals(""),二者的顺序不能交换,更不能用&运算符,因为第一个条件如 果不成立,根本不能进行字符串的 equals 比较

Equals for case class with floating point fields

拟墨画扇 提交于 2019-12-03 20:10:23
Is it ok, to create case classes with floating point fields, like: case class SomeClass(a:Double, b:Double) I guess auto generated equal method won't work in this case. Is overriding equals the best solution? EDIT: if overriding equals is the way to go, I would like to avoid hardcoding epsilon ( where epsilon is defined like => |this.a-a|< epsilon). This won't compile: case class SomeClass(a:Double, b:Double, implicit epsilon:Double) I am looking for a way to pass epsilon without passing concert value each time (some "implicit" magic). I have also follow up more general question, how would you

快速看懂HashMap

余生颓废 提交于 2019-12-03 19:49:14
在开始之前,先过一遍本博客的重点 • HashMap寻值的速度快是因为HashMap的键会被映射成Hash值,从而避开用equal方法遍历来加快寻值速度。 • HashMap有初始容量和加载因子两个参数来控制性能。当条目大于容量*加载因子时,容量翻一倍。 • HashMap和Hashtable的区别在于线程安全性,同步,以及速度。 下面开始正文 1.HashMap的具体实现 Java为数据结构中的映射定义了一个接口java.util.Map,此接口主要有四个常用的实现类,分别是HashMap、Hashtable、LinkedHashMap和TreeMap,类继承关系如下图所示: HashMap中我们最常用的就是put(K, V)和get(K)。我们都知道,HashMap的K值是唯一的,所以查询和修改数据的时候只要用equal遍历所有的key值就可以了,但我们知道直接遍历查询的时间复杂度为O(n),在数据量比较大时效率不高,所以在java中运用Hash算法来对所有的key进行运算加入数组中。查询的时候直接用数组下标来访问数据(时间复杂度变成了O(1)),以此绕开了用equal遍历带来的效率损失。 HashMap根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的。 HashMap最多只允许一条记录的键为null

为什么HashMap的Capacity必须2^n

泄露秘密 提交于 2019-12-03 19:45:23
为了元素均匀的分布在数组上,减少hash碰撞的机会。 一、hashmap数据结构 1、没有hash碰撞 hashmap初始化,或者put操作没有产生碰撞时,都是均匀分布在数组上。 2、hash碰撞 元素产生hash碰撞时,会调用equals方法比较两个元素是否相同,不相同则以链表的形式存储。 3、链表长度大于8 遍历链表寻找元素的时间复杂度是O(n),当链表长度大于8时,转换成功red black tree(红黑树), 时间复杂度O(lgn) 二、长度是2^n次方如何定位元素位置: hashMap的put操作是如何定位元素位置的,先求hash值。 static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } 1、hashcode的低位和高位(高位右移16位)做异或运算(相同是0,相异是1,任何与0做异或运算的结果都是0) 2、求hash值的过程 h=key.hashCode() h: 1111 1111 1111 1111 0000 0000 0000 1111 h>>>16(无符号右移16位,高位补0): 0000 0000 0000 0000 1111 1111 1111 1111 h^h>>>16 : 0000 0000

到底什么是hash呢?hash碰撞?为什么HashMap的初始容量是16?

元气小坏坏 提交于 2019-12-03 19:37:00
一 ,到底什么是hash呢? 作者:知乎用户 链接:https://www.zhihu.com/question/26762707/answer/40119521 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 hash(散列、杂凑)函数,是将任意长度的数据映射到有限长度的域上。直观解释起来,就是对一串数据m进行杂糅,输出另一段固定长度的数据h,作为这段数据的特征(指纹)。 也就是说,无论数据块m有多大,其输出值h为固定长度。到底是什么原理?将m分成固定长度(如128位),依次进行hash运算,然后用不同的方法迭代即可(如前一块的hash值与后一块的hash值进行异或)。如果不够128位怎么办?用0补全或者用1补全随意,算法中约定好就可以了。 原问题回答完毕。但是既然要说hash算法,不妨说的更透彻些。 =================分割线========== 由于用途的不同,hash在数据结构中的含义和密码学中的含义并不相同,所以在这两种不同的领域里,算法的设计侧重点也不同。 预备小知识: 抗碰撞能力:对于任意两个不同的数据块,其hash值相同的可能性极小;对于一个给定的数据块,找到和它hash值相同的数据块极为困难。 抗篡改能力:对于一个数据块,哪怕只改动其一个比特位,其hash值的改动也会非常大。 在用到hash进行管理的数据结构中

hashCode() method when equals() is based on multiple independent fields

自闭症网瘾萝莉.ら 提交于 2019-12-03 17:26:35
问题 i have a class whose equality is based on 2 fields such that if either one is equal then the objects of this type are considered equal. how can i write a hashCode() function for such an equals() so that the general contract of hashCode being equal when equals returns true is preserved? public class MyClass { int id; String name; public boolean equals(Object o) { if (!(o instanceof MyClass)) return false; MyClass other = (MyClass) o; if (other.id == this.id || other.name == this.name) return

Java中HashMap的实现原理

六月ゝ 毕业季﹏ 提交于 2019-12-03 16:57:02
一、Java中的hashCode和equals 1、关于hashCode hashCode的存在主要是用于查找的快捷性,如Hashtable,HashMap等,hashCode是用来在散列存储结构中确定对象的存储地址的 如果两个对象相同,就是适用于equals(java.lang.Object) 方法,那么这两个对象的hashCode一定要相同 如果对象的equals方法被重写,那么对象的hashCode也尽量重写,并且产生hashCode使用的对象,一定要和equals方法中使用的一致,否则就会违反上面提到的第2点 两个对象的hashCode相同,并不一定表示两个对象就相同,也就是不一定适用于equals(java.lang.Object) 方法,只能够说明这两个对象在散列存储结构中,如Hashtable,他们“存放在同一个篮子里“ 再归纳一下就是hashCode是用于查找使用的,而equals是用于比较两个对象的是否相等的。 以下对hashCode的解读摘自其他博客: 1.hashcode是用来查找的,如果你学过数据结构就应该知道,在查找和排序这一章有 例如内存中有这样的位置 0 1 2 3 4 5 6 7 而我有个类,这个类有个字段叫ID,我要把这个类存放在以上8个位置之一,如果不用hashcode而任意存放,那么当查找时就需要到这八个位置里挨个去找,或者用二分法一类的算法。

Example of ==, equals and hashcode in java

不问归期 提交于 2019-12-03 15:27:45
Given this: String s1= new String("abc"); String s2= new String("abc"); String s3 ="abc"; System.out.println(s1==s3); System.out.println(s1==s2); System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); System.out.println(s3.hashCode()); Output is: false false true true 96354 96354 96354 Here == is giving false for each object but the hashcode for each String object is same. Why is it so? == does compare real equality of objects (I mean - both references point to the same object), not their content, whereas