hashcode

Different fields for equals and hashcode

我的未来我决定 提交于 2019-12-04 15:40:44
I agree with the statement from this post What issues should be considered when overriding equals and hashCode in Java? Use the same set of fields that you use to compute equals() to compute hashCode(). But i've some doubts : Is this absolutely necessary to have same fields ? If yes, what if I don't use same field ? Will it affect HashMap performance or HashMap Accuracy ? The fields don't have to be the same. The requirement is for two objects that are equal, they must have the same hash code. If they have the same hash code, they don't have to be equal. From the javadocs: Whenever it is

Java开发常见基础题大全

℡╲_俬逩灬. 提交于 2019-12-04 11:30:59
1.& 和 && 的区别? & :逻辑与( and ) , 运算符两边的表达式均为 true 时,整个结果才为 true 。 && :短路与,如果第一个表达式为 false 时,第二个表达式就不会计算了。 2. ” == ”和 equals 方法究竟有什么区别? == :表示两个变量的值是否相等,用于比较两个基本数据类型的数据或者引用变量。 equals: 用于比较两个独立对象的内容是否相同。字符串的比较也用 equals 。 == 对于基本类型来说是值比较,对于引用类型来说是比较的是引用;而 equals 默认情况下是引用比较,只是很多类重新了 equals 方法,比如 String 、 Integer 等把它变成了值比较,所以一般情况下 equals 比较的是值是否相等。 3. 比较字符串相等时用 equals 和 == 的区别 String 是对象,如果用 == 号就是比较两个 String 对象内存地址是否一样, equals() 比较的是 String 内容时候一样,不过不同的编程语言两者不太一样 4.Int 和 integer 的区别? Int 是 Java 的 8 中基本数据类型之一, integer 是 int 的封装类。 Int 类型的默认值为 0 , integer 默认值为 null ,所以区别在于, integer 能区分出 null 值和 0 的区别。 5

Java String hashcode caching

橙三吉。 提交于 2019-12-04 10:36:18
One of the advantages of String immutability is hashcode caching for faster access. In this case how cache is handled for string which has same hashcode? Does it really improve the performance in this case? In this case how cache is handled for string which has same hashcode? I don't understand the first part of your question. Cache is handled for all Strings the same, whether hashcodes are the same or not (since two different Strings can theoretically have the same hashCode, and so if hashCodes are equal, it doesn't mean that the Strings are equal). But if the same String object is used, the

.Net GetHashcode Bit Shifting Operation

和自甴很熟 提交于 2019-12-04 10:26:16
问题 I was looking through some of the .net source yesterday and saw several implementations of GetHashcode with something along the lines of this: (i1 << 5) + i ^ i2 I understand what the code is doing and why. What I want to know is why they used (i1 << 5) + i instead of (i1 << 5) - i. Most frameworks I've seen use -i because that's equivalent to multiplying by 31 which is prime, but the Microsoft way is equivalent to multiplying by 33 which has 11 and 3 as factors and thus isn't prime. Is there

How likely is it to get a HashCode collision with this hashcode function?

送分小仙女□ 提交于 2019-12-04 10:11:41
How likely is it to get a HashCode collision with the function below in following scenarios. With random int values for key[0],key[1], key[2], key[3] With random key values with the following constraints key[0] <1,000,000 key[1] <10,000 key[2] <1,000 key[3] <1,000 Assume we have 10 Million objects. int[] key=new int[4]; public override int GetHashCode() { // Use large prime multiples to create a unique hash key // Create the hash offsets using a "even powers of 2 minus 1" method, which gives // primes most of the time. int hashKey = 0; hashKey += 2047 * key[0]; hashKey += 8191 * key[1];

HashMaps and Null values?

十年热恋 提交于 2019-12-04 09:51:44
问题 How do you pass in null values into a HashMap? The following code snippet works with options filled in: HashMap<String, String> options = new HashMap<String, String>(); options.put("name", "value"); Person person = sample.searchPerson(options); System.out.println(Person.getResult().get(o).get(Id)); So the issue is what has to be entered into the options and or method to pass in a null value? I tried the following code without any success: options.put(null, null); Person person = sample

How is base.GetHashCode() implemented for a struct? [duplicate]

佐手、 提交于 2019-12-04 09:03:40
This question already has an answer here: How does native implementation of ValueType.GetHashCode work? 3 answers I saw this code recently in a struct and I was wondering what base.GetHashCode actually does. public override int GetHashCode() { var hashCode = -592410294; hashCode = hashCode * -1521134295 + base.GetHashCode(); hashCode = hashCode * -1521134295 + m_Value.GetHashCode(); return hashCode; } The coreclr repo has this comment : Action: Our algorithm for returning the hashcode is a little bit complex. We look for the first non-static field and get it's hashcode. If the type has no non

Is there a way to auto-generate GetHashCode and Equals with ReSharper?

我与影子孤独终老i 提交于 2019-12-04 08:48:54
问题 In eclipse, when I code in Java, there is a feature to auto-generate a basic, efficient, and bug free implementation of hashCode() and equals() without consuming brain power. Is there a similar feature either built-in in Visual Studio or in ReSharper ? 回答1: Yes, Resharper can do that. With cursor inside your type, open the “Generate code” menu ( Alt + Ins depending on settings or Resharper -> Edit -> Generate Code ), and select “Equality members”: This opens a window where you can select

HashMap工作原理

房东的猫 提交于 2019-12-04 08:46:21
hashing的概念 HashMap中解决碰撞的方法 equals()和hashCode()的应用,以及它们在HashMap中的重要性 不可变对象的好处 HashMap多线程的条件竞争 重新调整HashMap的大小 “什么是HashMap?你为什么用到它?” 数组和链表组合成的链表散列结构,通过hash算法,尽量将数组中的数据分布均匀,如果hashcode相同再比较equals方法,如果equals方法返回false,那么就将数据以链表的形式存储在数组的对应位置,并将之前在该位置的数据往链表的后面移动,并记录一个next属性,来指示后移的那个数据。注意数组中保存的是entry,其中保存的是键值. HashMap可以接受null键值和值,而HashTable则不能;HashMap是非synchronized;HashMap很快;以及HashMap储存的是键值对等等 “为什么String, Interger这样的wrapper类适合作为键?” String, Interger这样的wrapper类作为HashMap的键是再适合不过了,而且String最为常用。因为String是不可变的,也是final的,而且已经重写了equals()和hashCode()方法了。其他的wrapper类也有这个特点。不可变性是必要的,因为为了要计算hashCode(),就要防止键值改变

If Java's garbage collector moves objects, what is Object.hashCode and System.identityHashCode?

醉酒当歌 提交于 2019-12-04 08:08:15
问题 I've often heard that these methods ( Object.hashCode and System.identityHashCode ) return the address of the object, or something computed quickly from the address; but I'm also pretty sure the garbage collector moves and compacts objects. Since the hash code cannot change, this presents a problem. I know this is not something one needs to know for everyday work, but I'd like to understand the internals. So, does anyone know how this is implemented in Java? Or .NET, since they are probably