equals

How use of == operator brings in performance improvements compared to equals?

痞子三分冷 提交于 2019-12-04 08:26:21
In Effective JAVA by Joshua Bloch, when I was reading about static factory methods , there was a statement as follows The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict control over what instances exist at any time. Classes that do this are said to be instance-controlled. There are several reasons to write instance-controlled classes. Instance control allows a class to guarantee that it is a singleton (Item 3) or noninstantiable (Item 4). Also, it allows an immutable class (Item 15) to make the guarantee that no two equal

Bit Array Equality

左心房为你撑大大i 提交于 2019-12-04 07:48:51
I need something a little more than the System.Collections.BitArray class in my application. Specifically, I need the bit array: To be immutable To implement equality using value semantics I created my own struct , largely copying the internals of the BitArray implementation. (Thanks, .Net Reflector !) I don't deal everyday with bitwise operations, so I don't have the highest degree of confidence in my equality implementation. (It's passing the unit tests I am throwing at it, but I may be missing edge cases.) I have my proposed solutions as answers below. I would appreciate others' feedback

EqualityComparer<T>.Default isn't clever enough

不想你离开。 提交于 2019-12-04 05:01:13
I was reading the source code of EqualityComparer<T>.Default and found that it's not so clever. Here is an example: enum MyEnum : int { A, B } EqualityComparer<MyEnum>.Default.Equals(MyEnum.A, MyEnum.B) //is as fast as EqualityComparer<int>.Default.Equals(0, 1) enum AnotherEnum : long { A = 1L, B = 2L } //is 8x slower than EqualityComparer<long>.Default.Equals(1L, 2L) The reason is obvious from the source code of the private method in EqualityComparer. private static EqualityComparer<T> CreateComparer() { //non-important codes are ignored if (c.IsEnum && (Enum.GetUnderlyingType(c) == typeof

Java contains doesn't work as expected because “someString” != “someString”

纵饮孤独 提交于 2019-12-04 03:35:08
问题 I want check whether a String value val is contained within a List of Strings lets call it stringList. I am doing this if(stringList.contains(val)){ System.out.println("The value is in there"); } else{ System.out.println("There's no such value here"); } But it always seems to be that the value is not included. Is this because two String values that have the same characters are not actually equal? For a "home-made" class I could implement hashCode() and equals() and fix this, what can I do for

Scala: lightweight way to put Arrays in a Set or Map

与世无争的帅哥 提交于 2019-12-04 03:21:44
Since == does not work with Arrays, I cannot effectively create a Set of Arrays (or Map with Array keys). I would rather not take the performance hit of converting my Arrays to a Vector or List or something. Is there a lightweight way to define natural comparison and hashcode on Arrays so I can stick them in a Set? Use WrappedArray from collection.mutable . It provides proper equality for arrays with a minimal overhead. apply , update etc calls are delegated to underlying array. Also there are special classes for primitive types (e.g. WrappedArray.ofInt ) to avoid boxing and unboxing. scala>

Equals method of System.Collections.Generic.List<T>…?

我们两清 提交于 2019-12-04 03:08:54
问题 I'm creating a class that derives from List... public class MyList : List<MyListItem> {} I've overridden Equals of MyListItem... public override bool Equals(object obj) { MyListItem li = obj as MyListItem; return (ID == li.ID); // ID is a property of MyListItem } I would like to have an Equals method in the MyList object too which will compare each item in the list, calling Equals() on each MyListItem object. It would be nice to simply call... MyList l1 = new MyList() { new MyListItem(1), new

Did I implement equals and hashCode correctly using Google Guava?

浪尽此生 提交于 2019-12-04 02:32:25
I am using hibernate and need to override equals and hashCode(). I chose to use google-guava's equals and hashCode helpers. I wanted to know if I am missing something here. I have get/set methods for idImage and filePath . @Entity @Table(name = "IMAGE") public class ImageEntity { private Integer idImage; private String filePath; @Override public int hashCode() { return Objects.hashCode(getFilePath()); } @Override public boolean equals(final Object obj) { if(obj == this) return true; if(obj == null) return false; if(obj instanceof ImageEntity){ final ImageEntity otherImage = (ImageEntity) obj;

Can Java help me avoid boilerplate code in equals()?

大憨熊 提交于 2019-12-04 02:26:54
I implement equals() the Java 7 way: @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; MyClass other = (MyClass) obj; return Objects.equal(myFirstField, other.myFirstField) && Objects.equal(mySecondField, other.mySecondField); } Is there a way to reduce the code duplication? I would prefer something like @Override public boolean equals(Object obj) { if (Objects.equalsEarlyExit(this, obj)) return Objects.equalstEarlyExitResult(this, obj); MyClass other = (MyClass) obj; return Objects.equal

What is the difference between Java's equals() and C++'s operator ==?

牧云@^-^@ 提交于 2019-12-04 02:22:32
In a question regarding the use of typeid is C++, I suggested it could be used to compare types in objects comparison. I haven't seen it done much, but I had Java's equals in mind. Looking into Java a bit more , this seems to be the case: Some say the actual classes of the two objects should be compared, and some say instanceof is the right tool to use, possibly with double dispatch. There are of course cases in which one of the two is definitively more suitable, but at least both options are considered . In C++, OTOH, I could barely find code in which the actual types are compared. On most

Determine if Equals() is an override?

北城余情 提交于 2019-12-04 01:42:31
问题 I have an instance of Type (type). How can I determine if it overrides Equals()? 回答1: private static bool IsObjectEqualsMethod(MethodInfo m) { return m.Name == "Equals" && m.GetBaseDefinition().DeclaringType.Equals(typeof(object)); } public static bool OverridesEqualsMethod(this Type type) { var equalsMethod = type.GetMethods() .Single(IsObjectEqualsMethod); return !equalsMethod.DeclaringType.Equals(typeof(object)); } Note that this reveals whether object.Equals has been overridden anywhere