Overriding Equals method in Structs
struct MyStruct { public override bool Equals(object obj) { if (!(obj is MyStruct)) return false; MyStruct mys = (MyStruct) obj; // compare elements here } }
struct MyStruct { public override bool Equals(object obj) { if (!(obj is MyStruct)) return false; MyStruct mys = (MyStruct) obj; // compare elements here } }
The key to the answer is called object interning. Java interns small numbers (less than 128), so all instances of Integer(n) with n in the interned range are the same. Numbers greater than or equal to 128 are not interned, hence Integer(1000) objects are not equal to each other.
The for loop at least can be streamified, leading to: return (list1.size()==list2.size() && IntStream.range(0, list1.size()) .allMatch(i -> Arrays.equals(list1.get(i), list2.get(i)));
Not necessarily. There are three options: don’t override – thus you will be working with instances. This is fine in cases when you are working with the collections with only entities that are attached to the session (and hence guaranteed to be the same instance). This is (for me) the preferred way in many cases, … Read more
Let’s say your class looks like this: class Frob { public string Foo { get; set; } public int Bar { get; set; } public double FooBar { get; set; } } Let’s say you define equals so that two instances of Frob are equal if their Foo and their Bar are equal, but FooBar … Read more
Children should not examine the private members of their parents But obviously, all significant fields should be taken into account for equality and hashing. Fortunately, you you can easily satisfy both rules. Assuming you’re not stuck using the NetBeans-generated equals and hashcode, you can modify Hominidae’s equals method to use instanceof comparison rather than class … Read more
The first thing you should note is that public boolean equals(Test testje) doesn’t override Object‘s equals, since the argument is Test instead of Object, so the signatures don’t match. Therefore the main method calls equals(Test testje) exactly once – when executing t3.equals(t3); – since that’s the only case in which both the static type of … Read more
Short answer: I think your second assumption may be flawed. Equals() is the right way to check for semantic equality of two objects, not operator ==. Long answer: Overload resolution for operators is performed at compile time, not run time. Unless the compiler can definitively know the types of the objects it’s applying an operator … Read more
First of all: that’s an interesting question, but should never come up in “real code”, as assigning to the variable you call in the very same line is confusing even if you know how it works. What happens here is these 3 steps: figure out which object to call the method on (i.e. evaluate the … Read more
To the question of whether this asymmetry is inconsistent, I think not, and I refer you to this ancient Zen kōan: Ask any man if he’s as good as the next man and each will say yes. Ask any man if he’s as good as nobody and each will say no. Ask nobody if it’s … Read more