equals

java Object类源代码详解 及nativ

前提是你 提交于 2019-11-28 20:23:12
Java代码 package java.lang; public class Object { /* 一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用。*/ private static native void registerNatives(); /* 对象初始化时自动调用此方法*/ static { registerNatives(); } /* 返回此 Object 的运行时类。*/ public final native Class<?> getClass(); /* hashCode 的常规协定是: 1.在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。 2.如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。 3.如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不 要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。 */

bash string equality [duplicate]

两盒软妹~` 提交于 2019-11-28 15:35:31
问题 This question already has an answer here: What is the difference between operator “=” and “==” in Bash? 2 answers In bash , what's the difference, if any, between the equal and double equal test operators? [[ "a" = "a" ]] && echo equal || echo not-equal [[ "a" == "a" ]] && echo equal || echo not-equal [[ "a" = "b" ]] && echo equal || echo not-equal [[ "a" == "b" ]] && echo equal || echo not-equal results in: equal equal not-equal not-equal 回答1: There's no difference, == is a synonym for =

Two identical Strings are not equal(Not pointer/reference mistake)

耗尽温柔 提交于 2019-11-28 12:47:37
I read a line from a file: KatalogObrazków 1 32 Means that I should look for data in: C:\Users\NAME_OF_THE_USER/KatalogObrazków and so I do it, but there is horrible thing going on. In splitLine[0] I have a word "KatalogObrazków" but then the computer says that "KatalogObrazków".equals(splitLine[0]) is false, there is no whitespace arround splitLine[0] left after splitting line. Please have a look at the code below. BufferedReader br = new BufferedReader(new FileReader(path)); String line; String[] splitLine; if ((line = br.readLine()) != null) { splitLine = line.split(" "); System.out.println

Using auto generated id of Hibernate entity object in the equals and hashcode methods

廉价感情. 提交于 2019-11-28 12:40:17
Lovely equals and hashcode, all the theory is here and also here I have taken the decision to use the auto-generated id within equals() and hashcode() in a number of my hibernate entity/domain objects. However, a number of websites say you should never do this due to the risk of persisting an object to the database for the first time whilst it is in the process of being compared or using hashcode. My point of view is that in most use cases this is much more unlikely than any other field being changed. The individual domain objects have the id generated once when they are first created, whereas

How are Equals and GetHashCode implemented on anonymous types?

天涯浪子 提交于 2019-11-28 12:36:52
The Help says this: Anonymous types are class types that derive directly from object, and that cannot be cast to any type except object. The compiler provides a name for each anonymous type, although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type. If two or more anonymous object initializers in an assembly specify a sequence of properties that are in the same order and that have the same names and types, the compiler treats the objects as instances of the same type. They share the same

Simplify Overriding Equals(), GetHashCode() in C# for Better Maintainability

心不动则不痛 提交于 2019-11-28 12:07:49
I find my self overriding Equals() and GetHashCode() frequently to implement the semantic that business objects with identical property values are equal. That leads to code that is repetitive to write and fragile to maintain (property gets added and one/both of the overrides are not updated). The code ends up looking something like this (comments on the implementation are welcome): public override bool Equals(object obj) { if (object.ReferenceEquals(this, obj)) return true; MyDerived other = obj as MyDerived; if (other == null) return false; bool baseEquals = base.Equals((MyBase)other); return

Prevent stubbing of equals method

折月煮酒 提交于 2019-11-28 12:05:49
I would like to test my class' equals() method but Mockito seems to be calling the stub version every time. My test is as follows; PluginResourceAdapter adapter = mock (PluginResourceAdapter.class); PluginResourceAdapter other = mock (PluginResourceAdapter.class); when(adapter.getNumberOfEndpointActivation()).thenReturn(1); when(other.getNumberOfEndpointActivation()).thenReturn(0); boolean result = adapter.equals(other); assertFalse(result); I know I cannot stub the equals method which means Mockito should be calling my real implementation but its not. I have also tried this: when (adapter

Java Hashset.contains() produces mysterious result

落爺英雄遲暮 提交于 2019-11-28 09:49:53
I don't usually code in Java, but recently I started not having a choice. I might have some major misunderstanding of how to properly use HashSet. So it might be possible something I did is just plain wrong. However I'm grateful for any help, you might offer. So the actual problem: In a small program I was writing, I was generating very similar objects, which, when created, would have a very specific id (a string or in my last iteration a long ). Because each object would spawn new objects, I wanted to filter out all those I already created. So I started throwing the id of every new object

What's wrong with defining operator == but not defining Equals() or GetHashCode()?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 09:42:51
For the code below public struct Person { public int ID; public static bool operator ==(Person a, Person b) { return a.Equals(b); } public static bool operator !=(Person a, Person b) { return !a.Equals(b); } } Why does the compiler give me these warnings? What's wrong with not defining the methods below? warning CS0660: 'Person' defines operator == or operator != but does not override Object.Equals(object o) warning CS0661: 'Person' defines operator == or operator != but does not override Object.GetHashCode() EDIT : This answer has been corrected, among other things to note that user-defined

Compare two objects with a check for null

≯℡__Kan透↙ 提交于 2019-11-28 09:35:28
Is there a method in the JDK that compares two objects for equality, accounting for nulls? Something like this: public static boolean equals(Object o1, Object o2) { if (o1 == null) { return o2 == null; // Two nulls are considered equal } else if (o2 == null) { return false; } return o1.equals(o2); } It seems silly to write this method myself since I would think that it has to exist already somewhere. Java 7.0 added a new handy class: Objects . It has a method exactly for this: Objects.equals(Object a, Object b) Yishai Apache Commons Lang has such a method: ObjectUtils.equals(object1, object2)