hashcode

Why was hashcode designed to return int [duplicate]

為{幸葍}努か 提交于 2019-12-13 05:17:52
问题 This question already has answers here : Why Object#hashCode() returns int instead of long (3 answers) Closed 6 years ago . It seems like a hashcode always returns an int. Now this appears to be a limiting factor due to IntMax. Now one can argue that such we would never have so many objects, as it would cause heap overflow etc. But if we chose double instead of int then we could guarantee with a much larger extent that hashcode wont be unique for distinct objects ? 回答1: Hashcodes don't need

Adding an object to a dictionary… but testing for partial uniqueness

爷,独闯天下 提交于 2019-12-13 01:33:24
问题 I have the following object and I want a dictionary to conditionally determine if there is a duplicate. For example, in one dictionary I only care about two properties being unique for my key. In a second dictionary, I want all the properties being unique for the key. Question 1: What interfaces should I override to accomplish this? (e.g. GetHashCode, IEqualityComparer, equals operator) Question 2: What should I do if I change a property that ultimately changes the value of the key? This is

深入浅出HashMap的设计与优化

空扰寡人 提交于 2019-12-13 00:50:09
一:常用的数据结构 众所周知, ArrayList 是基于数组的数据结构实现的,LinkedList 是基于链表的数据结构实现的,而 HashMap 是基于哈希表的数据结构实现的。我们不妨一起来温习下常用的数据结构。 数组: 采用一段连续的存储单元来存储数据。对于指定下标的查找,时间复杂度为 O(1),但在数组中间以及头部插入数据时,需要复制移动后面的元素。 链表: 一种在物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素)组成,结点可以在运行时动态生成。每个结点都包含“存储数据单元的数据域”和“存储下一个结点地址的指针域”这两个部分。由于链表不用必须按顺序存储,所以链表在插入的时候可以达到 O(1) 的复杂度,但查找一个结点或者访问特定编号的结点需要 O(n) 的时间。 哈希表: 根据关键码值(Key value)直接进行访问的数据结构。通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做哈希函数,存放记录的数组就叫做哈希表。 树: 由 n(n≥1)个有限结点组成的一个具有层次关系的集合,就像是一棵倒挂的树。 二:HashMap 的实现结构 了解完数据结构后,我们再来看下 HashMap 的实现结构。作为最常用的 Map 类,它是基于哈希表实现的,继承了 AbstractMap

Example to use a hashcode to detect if an element of a List<string> has changed C#

无人久伴 提交于 2019-12-12 23:31:37
问题 I have a List that updates every minute based on a Linq query of some XML elements. the xml changes, from time to time. It was suggested to me that I could use Hashcode to determine if any of the strings in the list have changed. I have seen some examples of Md5 hashcode calculations for just a string, but not for a list...could someone show me a way of doing this with a list? I tried something simple like int test = list1.GetHashCode; but the code is the same no matter what is in the list...

Is it possible to get String value back from its hash code?

狂风中的少年 提交于 2019-12-12 18:12:55
问题 Java doc for method String#hashCode() says: 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.) Questions: Is it possible to have same hash code for two string objects having different values? If yes then please share some examples. Is it

HashMap底层源码

China☆狼群 提交于 2019-12-12 12:39:15
​ 这里是修真院后端小课堂,每篇分享文从 本篇分享的是:【HashMap 】 (1)背景介绍: 不讲HashMap的使用方法,看一看底层的源码是什么? 思考:HashMap使用key,·value进行存储,使用的数据结构是什么? 我们知道数组和链表两种数据结构 数组: 优点:查询速度快 缺点:增加和删除慢 链表: 优点:增加和删除快 缺点:查询速度慢 那我们可不可以将两者的优点结合一下,达到查询、增加、删除效率都非常快呢? 所以我们猜测一下,HashMap能否源码底层数据结构是采用的链表+数组的形式呢? (2)知识剖析: 1.HashMap数据底层具体存储的是什么? Java是一门面向对象开发的语言,可以把所有东西可以看做是对象。 通过查看源码可知:map里面的key和value都保存在了这个Node对象里面。 class Node<K,V>{ private K key; //用来定位数组索引位置 private V value; private Node<K,V> next; //链表的下一个node } 2.数组怎么表示? transient Node< K,V>[] table; 每个列表被称为桶,即哈希桶数组,是一个Node的数组。 HashMap使用哈希表进行存储。怎样得到表中对象的索引位置? key.hashCode( )----->hashCode-----

Inconsistency in Equals and GetHashCode methods

£可爱£侵袭症+ 提交于 2019-12-12 09:33:49
问题 After reading this question Why do "int" and "sbyte" GetHashCode functions generate different values? I wanted to dig further and found following behavior: sbyte i = 1; int j = 1; object.Equals(i, j) //false (1) object.Equals(j, i) //false (2) i.Equals(j) //false (3) j.Equals(i) //true (4) i == j //true (5) j == i //true (6) i.GetHashCode() == j.GetHashCode() //false (7) Difference between (3) and (4) breaks the requirement that Equals should be symmetric. Difference between (2) and (4) is

HashMap源码学习

…衆ロ難τιáo~ 提交于 2019-12-12 08:49:47
HashMap 简介 HashMap 主要用来存放键值对,它基于哈希表的Map接口实现,是常用的Java集合之一。 JDK1.8 之前 HashMap 由 数组+链表 组成的,数组是 HashMap 的主体,链表则是主要为了解决哈希冲突而存在的(“拉链法”解决冲突).JDK1.8 以后在解决哈希冲突时有了较大的变化,当链表长度大于阈值(默认为 8)时,将链表转化为红黑树(将链表转换成红黑树前会判断,如果当前数组的长度小于 64,那么会选择先进行数组扩容,而不是转换为红黑树),以减少搜索时间,具体可以参考 treeifyBin 方法。 底层数据结构分析 JDK1.8之前 JDK1.8 之前 HashMap 底层是 数组和链表 结合在一起使用也就是 链表散列 。 HashMap 通过 key 的 hashCode 经过扰动函数处理过后得到 hash 值,然后通过 (n - 1) & hash 判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存入的元素的 hash 值以及 key 是否相同,如果相同的话,直接覆盖,不相同就通过拉链法解决冲突。 所谓扰动函数指的就是 HashMap 的 hash 方法。使用 hash 方法也就是扰动函数是为了防止一些实现比较差的 hashCode() 方法 换句话说使用扰动函数之后可以减少碰撞 JDK 1

what is the preferred way of implementing hashCode()?

醉酒当歌 提交于 2019-12-12 08:38:01
问题 Sometimes I need to implement an obj's hashCode() method by combining the hashCodes of its several instance members. For example, if the combinational obj has members a, b, and c, I often see ppl implement it as int hashCode(){ return 31 * 31 * a.hashCode() + 31 * b.hashCode() + c.hashCode(); } Where does this magic number 31 come from? Is it the length of 4-bytes or just a prime number? Is there any other preferred/standard way of implementing hashCode()? 回答1: See Effective Java's recipe. It

java中String、StringBuffer和StringBuilder的区别(简单介绍)

老子叫甜甜 提交于 2019-12-12 08:00:15
java中String、StringBuffer和StringBuilder的区别(简单介绍) 简单介绍 java中用于处理字符串常用的有三个类: 1、java.lang.String 2、java.lang.StringBuffer 3、java.lang.StrungBuilder 三者共同之处:都是final类,不允许被继承,主要是从性能和安全性上考虑的,因为这几个类都是经常被使用着,且考虑到防止其中的参数被参数修改影响到其他的应用。 StringBuffer是线程安全,可以不需要额外的同步用于多线程中; StringBuilder是非同步,运行于多线程中就需要使用着单独同步处理,但是速度就比StringBuffer快多了; StringBuffer与StringBuilder两者共同之处:可以通过append、indert进行字符串的操作。 String实现了三个接口:Serializable、Comparable<String>、CarSequence StringBuilder只实现了两个接口Serializable、CharSequence,相比之下String的实例可以通过compareTo方法进行比较,其他两个不可以。 这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面。   1、首先说运行速度,或者说是执行速度, 在这方面运行速度快慢为