Questions tagged [equality]

Equality is a relationship between two or more items or variables or objects that exists if (1) the items are the same item, variable, or object or (2) the items are different items, variables or objects but they have the same value. This tag should generally be used with programming language specific tags as well as other contextual tags such as database system. The post should include as much context about the equality test as is possible.

This tag is typically used for questions concerning how to determine if two objects or variables are either the same object or different objects with the same value. Questions almost always require a programming language tag and other tags for context.

Programming languages represent variables, objects, or other storage containers using different underlying mechanisms for the storage of the values of variables and objects. A test for equality is to compare some characteristic of two variables or objects to determine if the characteristic is the same.

This characteristic is typically either (1) the storage identifier such as a reference handle, memory address, etc. when checking if the two are the same object or (2) the characteristic is the value of the objects retrieved using a storage identifier when checking if the value of the two are the same value.

Different programming languages and different operating environments have differences in how equality is tested and what operators are used to express a test for equality. In some cases various conversions from underlying storage representations may be involved in testing for equality.

See What is the difference between equality and equivalence? as well as Equivalence relation and natural ordering on a class in Java

2095 questions
65
votes
6 answers

How to test for (ActiveRecord) object equality

In Ruby 1.9.2 on Rails 3.0.3, I'm attempting to test for object equality between two Friend (class inherits from ActiveRecord::Base) objects. The objects are equal, but the test fails: Failure/Error: Friend.new(name: 'Bob').should…
ryonlife
  • 6,563
  • 14
  • 51
  • 64
64
votes
5 answers

Comparing two string arrays in C#

Say we have 5 string arrays as such: string[] a = {"The","Big", "Ant"}; string[] b = {"Big","Ant","Ran"}; string[] c = {"The","Big","Ant"}; string[] d = {"No","Ants","Here"}; string[] e = {"The", "Big", "Ant", "Ran", "Too", "Far"}; Is there a…
Wes Field
  • 3,291
  • 6
  • 23
  • 26
63
votes
2 answers

How do I check if two variables reference the same object in Python?

x and y are two variables. I can check if they're equal using x == y, but how can I check if they have the same identity? Example: x = [1, 2, 3] y = [1, 2, 3] Now x == y is True because x and y are equal, however, x and y aren't the same…
snakile
  • 52,936
  • 62
  • 169
  • 241
63
votes
6 answers

What problem does IStructuralEquatable and IStructuralComparable solve?

I've noticed these two interfaces, and several associated classes, have been added in .NET 4. They seem a bit superfluous to me; I've read several blogs about them, but I still can't figure out what problem they solve that was tricky before .NET…
thecoop
  • 45,220
  • 19
  • 132
  • 189
62
votes
3 answers

Sets, Functors and Eq confusion

A discussion came up at work recently about Sets, which in Scala support the zip method and how this can lead to bugs, e.g. scala> val words = Set("one", "two", "three") scala> words zip (words map (_.length)) res1: Set[(java.lang.String, Int)] =…
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
61
votes
5 answers

Compare if two dataframe objects in R are equal?

How do I check if two objects, e.g. dataframes, are value equal in R? By value equal, I mean the value of each row of each column of one dataframe is equal to the value of the corresponding row and column in the second dataframe.
mindless.panda
  • 4,014
  • 4
  • 35
  • 57
59
votes
3 answers

Are objects with the same id always equal when comparing them with ==?

If I have two objects o1 and o2, and we know that id(o1) == id(o2) returns true. Then, does it follow that o1 == o2 Or is this not always the case? The paper I'm working on says this is not the case, but in my opinion it should be true!
Jonas Kaufmann
  • 1,797
  • 3
  • 22
  • 43
57
votes
2 answers

Why does the "is" keyword have a different behavior when there is a dot in the string?

Consider this code: >>> x = "google" >>> x is "google" True >>> x = "google.com" >>> x is "google.com" False >>> Why is it like that? To make sure the above is correct, I have just tested on Python 2.5.4, 2.6.5, 2.7b2, Python 3.1 on windows and…
YOU
  • 120,166
  • 34
  • 186
  • 219
56
votes
2 answers

Java contains vs anyMatch behaviour

If I have a Name object and have an ArrayList of type Name (names), and I want to ascertain whether my list of names contains a given Name object (n), I could do it two ways: boolean exists = names.contains(n); or boolean exists =…
Tranquility
  • 3,061
  • 5
  • 23
  • 37
56
votes
3 answers

What's the right way to implement equality in ruby

For a simple struct-like class: class Tiger attr_accessor :name, :num_stripes end what is the correct way to implement equality correctly, to ensure that ==, ===, eql?, etc work, and so that instances of the class play nicely in sets, hashes,…
Pete Hodgson
  • 15,644
  • 5
  • 38
  • 46
55
votes
2 answers

Case insensitive string comparison in Go

How do I compare strings in a case insensitive manner? For example, "Go" and "go" should be considered equal.
user7610
  • 25,267
  • 15
  • 124
  • 150
53
votes
3 answers

How to check if two Expression> are the same

Is it possible to find out if two expressions are the same? Like given the following four expressions: Expression> a = x => false; Expression> b = x => false; Expression> c = x…
Svish
  • 152,914
  • 173
  • 462
  • 620
52
votes
7 answers

Why does new String('hello') === new String('hello') evaluate to False?

Why does the following statement return false in JavaScript? new String('hello') === new String('hello')
51
votes
1 answer

Custom Equality check for C# 9 records

From what I understand, records are actually classes that implement their own equality check in a way that your object is value-driven and not reference driven. In short, for the record Foo that is implemented like so: var foo = new Foo { Value =…
panosru
  • 2,709
  • 4
  • 33
  • 39
50
votes
3 answers

Is it possible to define equality for named types/structs?

After reading a related question about using slices in maps, I became curious about equality in Go. I know it's possible to override the equals method of a Java Object. Is there a similar way to define how Go checks user defined types/structs for…
Bill DeRose
  • 2,330
  • 3
  • 25
  • 36