equals

Case insensitive string comparison in C++

What is the best way of doing case insensitive string comparison in C++ with out transforming a string to all upper or lower case? Also, what ever methods you present, are they Unicode friendly? Are they portable? ...

What is the "best" canonical implementation of Equals() for reference types?

Implementing Equals() for reference types is harder than it seems. My current canonical implementation goes like this: public bool Equals( MyClass obj ) { // If both refer to the same reference they are equal. if( ReferenceEquals( obj, this ) ) return true; // If the other object is null they are not equal because in C# this ...

Best way to handle null when writing equals operator

When I overload the == operator for objects I typically write something like this: public static bool operator ==(MyObject uq1, MyObject uq2) { if (((object)uq1 == null) || ((object)uq2 == null)) return false; return uq1.Field1 == uq2.Field1 && uq1.Field2 == uq2.Field2; } If you don't down-cast to object the function recurses in...

Java equals(): to reflect or not to reflect

This question is specifically related to overriding the equals() method for objects with a large number of fields. First off, let me say that this large object cannot be broken down into multiple components without violating OO principles, so telling me "no class should have more than x fields" won't help. Moving on, the problem came...

How to compute the hashCode() from the object's address?

In Java, I have a subclass Vertex of the Java3D class Point3f. Now Point3f computes equals() based on the values of its coordinates, but for my Vertex class I want to be stricter: two vertices are only equal if they are the same object. So far, so good: class Vertex extends Point3f { // ... public boolean equals(Object other) ...

Is there a Java standard "both null or equal" static method?

To save some typing and clarify my code, is there a standard version of the following method? public static boolean bothNullOrEqual(Object x, Object y) { return ( x == null ? y == null : x.equals(y) ); } ...

Overriding the java equals() method quirk

I ran into an interesting (and very frustrating) issue with the equals() method today which caused what I thought to be a well tested class to crash and cause a bug that took me a very long time to track down. Just for completeness, I wasn't using an IDE or debugger - just good old fashioned text editor and System.out's. Time was very ...

Error with no HashCode, Equals eclipse

Hi, I'm looking for a very specific eclipse plugin that will tell me if a class in my project is not implementing hashCode or/and equals methods. Does anyone know of such a plugin? Thanks ...

Can the behavior for == be defined for an interface reference?

If an interface inherits IEquatable the implementing class can define the behavior of the Equals method. Is it possible to define the behavior of == operations? public interface IFoo : IEquatable {} public class Foo : IFoo { // IEquatable.Equals public bool Equals(IFoo other) { // Compare by value here...

How to avoid, that URL.equals needs access to the internet in Java?

The equals()-method of the URL-class in the Java-class-library makes a DNS-request to get the IP for the hostname, to check the two IP's for equality. This happens even for URL's, that are created from the same String. Is there a way to avoid this internet-access? ...

Why Type.Equals(t1, t2) and not the equality operator?

Why must Type.Equals(t1, t2) be used to determine equivalent types, and not the equality operator (e.g. for VB.NET, t1 = t2)? It seems inconsistent with other parts of the .NET API. Example in VB.NET: If GetType(String) = GetType(String) Then Debug.Print("The same, of course") End If causes a compile-time error of "Operator '=...

How to compare two elements of the same but unconstrained generic type for equality?

I've got the following generic class and the compiler complains that "Operator '!=' cannot be applied to operands of type 'TValue' and 'TValue'" (see CS0019): public class Example<TValue> { private TValue _value; public TValue Value { get { return _value; } set { if (_value != value) // <<...

Java Tag Library for compairing two objects via the equals method.

In my JSP I'm required to compare to Objects to see if they are equal or not using the Object's equals method. Is there a way to do this using the JSTL or another Tag Library? (I am not allowed to use Scriptlets due to team rules.) I tried to use the JSTL tag, but it only seems to use the == operator. Any suggestions are appreciated...

What are the alternatives to comparing the equality of two objects?

http://leepoint.net/notes-java/data/expressions/22compareobjects.html It turns out that defining equals() isn't trivial; in fact it's moderately hard to get it right, especially in the case of subclasses. The best treatment of the issues is in Horstmann's Core Java Vol 1. If equals() must always be overridden, then what i...

Findbugs warning: Equals method should not assume anything about the type of its argument

When running FindBugs on my project, I got a few instances of the error described above. Namely, my overriding versions of equals cast the RHS object into the same type as the object in which the overriding version is defined. However, I'm not sure whether a better design is possible, since AFAIK Java does not allow variance in method ...

Generated equals in Webservice Stub

All the generated webservice-stubs from our backend have an equals-method similar to this one: private java.lang.Object __equalsCalc = null; public synchronized boolean equals(java.lang.Object obj) { if (!(obj instanceof PropertyData)) return false; PropertyData other = (PropertyData) obj; if (obj == null) return false; ...

Where is the implementation of InternalEquals(object objA, object objB)

Hello Friends while diassembling the .Net Source Code using Reflector, I came upon the Equals implementation in the Object Class and it refers to bool InternalEquals(object objA, object objB); Which again refers to internal static extern bool InternalEquals(object objA, object objB); I am now confused regarding where to find the...

How to ensure hashCode() is consistent with equals()?

When overriding the equals() function of java.lang.Object, the javadocs suggest that, "it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes." The hascode method must retur...

How can you define how objects are compared in JavaScript

I want to define a JavaScript class, Foo. Foo = function(value){ this.value = value; }; I will create "instances" of my Foo: foo1 = new Foo(1); foo2 = new Foo(1); and I want my instances of Foo to be comparable with each other using the standard == equality operator: foo1 == foo2; // this should be true I can not find ...

Why is Long.valueOf(0).equals(Integer.valueOf(0)) false?

This questions is prompted by http://stackoverflow.com/questions/444638/strange-hashmap-put-behaviour#444757 I think I understand why Map<K,V>.put takes a K but Map<K,V>.get takes an Object, it seems not doing so will break too much existing code. Now we get into a very error-prone scenario: java.util.HashMap<Long, String> m = new ja...