hashcode

Java: Automatic equals() and hashCode()

末鹿安然 提交于 2019-12-21 03:36:29
问题 Implementing equals() and hashCode() for simple data POJOs is cluttering my code and maintaining is tedious. What are the libraries handling this automatically? I prefer bytecode instrumentation over AOP approach due to performance reasons. Update: Topic of necessity of implementing equals() and hashCode() has been discussed, here's my point: Isn't it better to have it done right upfront with minimal effort rather than digging in the code, adding hC/eq when it comes to it? 回答1: Project Lombok

What is the purpose of hashcode method in java? [duplicate]

霸气de小男生 提交于 2019-12-21 03:33:06
问题 This question already has answers here : What is the use of hashCode in Java? (8 answers) Closed 4 years ago . When we have equals() , compareTo() methods why there is a hashcode() method in Java? And in case if we use HashTable we have to override hashcode() method, Is there any special reason except fast accessing of random keys? If we override the hashcode() method what would be the probable implementation ? How Java ensures object uniqueness in memory? Hashcodes are typically used to

java中hashcode和equals的区别和联系

旧巷老猫 提交于 2019-12-21 02:54:53
HashSet和HashMap一直都是JDK中最常用的两个类,HashSet要求不能存储相同的对象,HashMap要求不能存储相同的键。 那么Java运行时环境是如何判断HashSet中相同对象、HashMap中相同键的呢?当存储了“相同的东西”之后Java运行时环境又将如何来维护呢? 在研究这个问题之前,首先说明一下JDK对equals(Object obj)和hashcode()这两个方法的定义和规范: 在Java中任何一个对象都具备equals(Object obj)和hashcode()这两个方法,因为他们是在Object类中定义的。 equals(Object obj)方法用来判断两个对象是否“相同”,如果“相同”则返回true,否则返回false。 hashcode()方法返回一个int数,在Object类中的默认实现是“将该对象的内部地址转换成一个整数返回”。 接下来有两个个关于这两个方法的重要规范(我只是抽取了最重要的两个,其实不止两个): 规范1:若重写equals(Object obj)方法,有必要重写hashcode()方法,确保通过equals(Object obj)方法判断结果为true的两个对象具备相等的hashcode()返回值。说得简单点就是:“如果两个对象相同,那么他们的hashcode应该 相等”。不过请注意:这个只是规范

What is the best way to determine if Object has been changed?

南楼画角 提交于 2019-12-21 02:46:39
问题 I made some "save" bean functionality in my Java Web Application (JSF1.2, RichFaces). It is using JAXB to turn it into an XML string and then it is stored in the database. If the user loads this back, I want to notify the user if the (bean)content is changed and it should be saved again. My idea is to override the hashCode() function 'with' org.apache.commons.lang.builder.HashCodeBuilder , however I have lots of fields and child elements. Is there any other way to handle this kind of

java知识回顾

佐手、 提交于 2019-12-20 20:24:24
一、构造方法能不能被继承   当然不能,1.构造方法是类的唯一入口        2.构造方法与类名相同        3.子类构造方法中隐式的调用了父类的构造方法 二、值传递和引用传递、不变类和可变类、直接赋值和浅拷贝和深拷贝    1.直接赋值:在java中,对象的传递、方法参数的传递(将实参赋值给行参)多数是通过=来直接赋值的 值传递是值的copy 引用传递是引用的copy,即内存地址的copy 引用传递又分两种 1.如果是不变类 对引用的操作不会改变原引用地址,但会改变现引用地址的值,他们指向不同的对象(new对象除外)    2.如果是可变类 对引用的操作不会改变原引用地址,但会改变对象的值,他们指向的是同一个对象(new对象除外)            3.不管是可变不可变,只要使用new来重新赋值,则是一个新的引用与对象,不会修改原来的引用与对象 2.浅拷贝:对象实现cloneable接口,重写clone类,可以拷贝对象内的基本数据类型以及不变类型,引用类型不会被拷贝    3.深拷贝:对象中的引用类型也实现cloneable接口。。。         修改引用时new新的对象,如get、set时         序列化(腌酸菜、冷藏)    可变类:提供修改自身的方法,不可变类:要修改对象时必须new一个对象,如 String、基本类型的包装类、BigInteger

HashMap实现原理及源码分析

こ雲淡風輕ζ 提交于 2019-12-20 10:14:27
HashMap 概述 HashMap 是基于哈希表的 Map 接口的非同步实现。此实现提供所有可选的映射操作,并允许使用 null 值和 null 键。此类不保证映射的顺序,特别是它不保证该顺序恒久不变。 此实现假定哈希函数将元素适当地分布在各桶之间,可为基本操作(get 和 put)提供稳定的性能。迭代 collection 视图所需的时间与 HashMap 实例的“容量”(桶的数量)及其大小(键-值映射关系数)成比例。所以,如果迭代性能很重要,则不要将初始容量设置得太高或将加载因子设置得太低。也许大家开始对这段话有一点不太懂,不过不用担心,当你读完这篇文章后,就能深切理解这其中的含义了。 需要注意的是:Hashmap 不是同步的,如果多个线程同时访问一个 HashMap,而其中至少一个线程从结构上(指添加或者删除一个或多个映射关系的任何操作)修改了,则必须保持外部同步,以防止对映射进行意外的非同步访问。 HashMap 的数据结构 在 Java 编程语言中,最基本的结构就是两种,一个是数组,另外一个是指针(引用),HashMap 就是通过这两个数据结构进行实现。 HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体。 从上图中可以看出,HashMap 底层就是一个数组结构,数组中的每一项又是一个链表。当新建一个 HashMap 的时候,就会初始化一个数组。

Why do 2 delegate instances return the same hashcode?

一笑奈何 提交于 2019-12-20 08:57:28
问题 Take the following: var x = new Action(() => { Console.Write("") ; }); var y = new Action(() => { }); var a = x.GetHashCode(); var b = y.GetHashCode(); Console.WriteLine(a == b); Console.WriteLine(x == y); This will print: True False Why is the hashcode the same? It is kinda surprising, and will make using delegates in a Dictionary as slow as a List (aka O(n) for lookups). Update: The question is why. IOW who made such a (silly) decision? A better hashcode implementation would have been:

How to properly implement equals in Java

笑着哭i 提交于 2019-12-20 05:55:57
问题 I need to implement the equals method in some class A. Class A has an orderer collection of Enum type, and the behaviour I want to achive is that equals returns true for two instances of Class A that have exactly the same Enum values in the collection (in exactly the same positions of the collection). As I'm new to java, I'm having problems with this, and I dont know how to properly implement equals or the hashcode methods, so any help would be good :) 回答1: If you're using eclipse (netbeans

Same GetHashCode() for different objects

假装没事ソ 提交于 2019-12-20 04:15:34
问题 After executing this piece of code: int a = 50; float b = 50.0f; Console.WriteLine(a.GetHashCode() == b.GetHashCode()); We get False , which is expected, since we are dealing with different objects, hence we should get different hashes. However, if we execute this: int a = 0; float b = 0.0f; Console.WriteLine(a.GetHashCode() == b.GetHashCode()); We get True . Both obejcts return the same hash code: 0 . Why does this happen? Aren't they supposed to return different hashes? 回答1: The GetHashCode

generating an safe hashcode for an objectgraph

旧巷老猫 提交于 2019-12-20 03:33:12
问题 I am importing some data from an file (xls, csv, xml) wich will result in a complex in-memory object graph. Now I need to know whether this graph has been modified since it once was exported. What would be a safe way to check this? I suppose I'd export a hashcode with the file? If so would the standard way of generating an object's hashcode suffice? How should I generate the hash? I would prefer to generate the hash on the object graph rather than on the actual stream/file. 回答1: You can