hashcode

Why does String.hashCode() in Java have many conflicts? [closed]

断了今生、忘了曾经 提交于 2019-12-17 05:50:56
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Why does String.hashcode() have so many conflicts? I'm reading the String.hashCode() in jdk1.6, below is the codes public int hashCode

Why should I override hashCode() when I override equals() method?

情到浓时终转凉″ 提交于 2019-12-17 04:30:46
问题 Ok, I have heard from many places and sources that whenever I override the equals() method, I need to override the hashCode() method as well. But consider the following piece of code package test; public class MyCustomObject { int intVal1; int intVal2; public MyCustomObject(int val1, int val2){ intVal1 = val1; intVal2 = val2; } public boolean equals(Object obj){ return (((MyCustomObject)obj).intVal1 == this.intVal1) && (((MyCustomObject)obj).intVal2 == this.intVal2); } public static void main

How to create a HashMap with two keys (Key-Pair, Value)?

被刻印的时光 ゝ 提交于 2019-12-17 04:11:13
问题 I have a 2D array of Integers. I want them to be put into a HashMap. But I want to access the elements from the HashMap based on Array Index. Something like: For A[2][5], map.get(2,5) which returns a value associated with that key. But how do I create a hashMap with a pair of keys? Or in general, multiple keys: Map<((key1, key2,..,keyN), Value) in a way that I can access the element with using get(key1,key2,...keyN). EDIT : 3 years after posting the question, I want to add a bit more to it I

How do you get the “object reference” of an object in java when toString() and hashCode() have been overridden?

我们两清 提交于 2019-12-17 03:55:13
问题 I would like to print the "object reference" of an object in Java for debugging purposes. I.e. to make sure that the object is the same (or different) depending on the situation. The problem is that the class in question inherits from another class, which has overriden both toString() and hashCode() which would usually give me the id. Example situation: Running a multi-threaded application, where I (during development) want to check if all the threads use the same instance of a resource

How do you get the “object reference” of an object in java when toString() and hashCode() have been overridden?

此生再无相见时 提交于 2019-12-17 03:55:04
问题 I would like to print the "object reference" of an object in Java for debugging purposes. I.e. to make sure that the object is the same (or different) depending on the situation. The problem is that the class in question inherits from another class, which has overriden both toString() and hashCode() which would usually give me the id. Example situation: Running a multi-threaded application, where I (during development) want to check if all the threads use the same instance of a resource

GetHashCode Guidelines in C#

拥有回忆 提交于 2019-12-17 03:22:24
问题 I read in the Essential C# 3.0 and .NET 3.5 book that: GetHashCode()’s returns over the life of a particular object should be constant (the same value), even if the object’s data changes. In many cases, you should cache the method return to enforce this. Is this a valid guideline? I have tried a couple built-in types in .NET and they didn't behave like this. 回答1: The answer is mostly, it is a valid guideline, but perhaps not a valid rule. It also doesn't tell the whole story. The point being

Quick and Simple Hash Code Combinations

≯℡__Kan透↙ 提交于 2019-12-17 03:00:44
问题 Can people recommend quick and simple ways to combine the hash codes of two objects. I am not too worried about collisions since I have a Hash Table which will handle that efficiently I just want something that generates a code quickly as possible. Reading around SO and the web there seem to be a few main candidates: XORing XORing with Prime Multiplication Simple numeric operations like multiplication/division (with overflow checking or wrapping around) Building a String and then using the

Consistency of hashCode() on a Java string

会有一股神秘感。 提交于 2019-12-17 02:42:21
问题 The hashCode value of a Java String is computed as (String.hashCode()): s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] Are there any circumstances (say JVM version, vendor, etc.) under which the following expression will evaluate to false? boolean expression = "This is a Java string".hashCode() == 586653468 Update #1: If you claim that the answer is "yes, there are such circumstances" - then please give a concrete example of when "This is a Java string".hashCode() != 586653468. Try to be as

HashMap 的底层原理

走远了吗. 提交于 2019-12-17 01:02:12
HashMap 的底层原理 1. HashMap的数据结构 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大。但数组的二分查找时间复杂度小,为O(1);数组的特点是:寻址容易,插入和删除困难; 链表 链表存储区间离散,占用内存比较宽松,故空间复杂度很小,但时间复杂度很大,达O(N)。链表的特点是:寻址困难,插入和删除容易。 哈希表 哈希表((Hash table)既满足了数据的查找方便,同时不占用太多的内容空间,使用也十分方便。哈希表是由数组+链表组成的。例如:一个长度为16的数组中,每个元素存储的是一个链表的头结点。那么这些元素是按照什么样的规则存储到数组中呢。一般情况是通过hash(key)%len获得,也就是元素的key的哈希值对数组长度取模得到。比如哈希表中,12%16=12,28%16=12,108%16=12,140%16=12。所以12、28、108以及140都存储在数组下标为12的位置。 HashMap其实也是一个线性的数组实现的,所以可以理解为其存储数据的容器就是一个线性数组。这可能让我们很不解,一个线性的数组怎么实现按键值对来存取数据呢?这里HashMap有做一些处理。 首先HashMap里面实现一个静态内部类Entry,其重要的属性有 key , value, next,从属性key

Object有哪些方法

不打扰是莪最后的温柔 提交于 2019-12-16 23:43:25
object类的一些常用方法: 1 public final Class<?> getClass() 获取当期运行时的类。 public int hashCode() 返回该对象的哈希码值。支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能。 hashCode 的常规协定是: 在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。 如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。 如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法 不 要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。 实际上,由 Object 类定义的 hashCode 方法确实会针对不同的对象返回不同的整数。(这一般是通过将该对象的内部地址转换成一个整数来实现的,但是 Java TM 编程语言不需要这种实现技巧。) public boolean