Is this valid?
public struct MyStruct
{
public int Foo { get; set; }
public static bool operator ==(MyStruct a, MyStruct b)
{
return a.Equals(b);
}
public static bool operator !=(MyStruct a, MyStruct b)
{
return !a.Equals(b);
}
}
(I know it's slightly inefficient because Object.Equals uses...
Hi,
There is a Java bean object which has implemented equals function based on certain criteria (Criteria A). I have a requirement to identify unique objects based on another criteria (Criteria B). Since the equals function uses criteria A, I can not use HashSet. So I thought of using TreeSet with my custom Comparator which is based on ...
Yet another list-comparing question.
I have:
List<MyType> list1;
List<MyType> list2;
I need to check that they both have the same elements, regardless of their position within the list. Each MyType object may appear multiple times on a list. Is there a built-in function that checks this? What if I guarantee that each element appears on...
Are they really same? Today, I ran into this problem. Here is the dump from the Immediate Window:
?s
"Category"
?tvi.Header
"Category"
?s == tvi.Header
false
?s.Equals(tvi.Header)
true
?s == tvi.Header.ToString()
true
So, both s and tvi.Header contain "Category", but == returns false and Equals returns true.
s is defined as...
I know this has been asked before, but in spite of recommendations to use .equals() instead of the == comparison operator, I found that == works all the time:
String s1 = "Hello";
String s2 = "Hello";
System.out.println(s1 == s2); // true
Can anyone give me an example of the == operator failing?
...
I got hit by a strange "asymmetry" in C# that I do not really understand. See the following code:
using System;
using System.Diagnostics;
namespace EqualsExperiment
{
class Program
{
static void Main(string[] args)
{
object apple = "apple";
object orange = string.Format("{0}{1}", "ap", "pl...
I have some simple code that is comparing two float values to illustrate a problem I see with GCC's optimization and am hoping someone can help me figure out why the output it produces is different under some repeatable circumstances.
First, I know it's bad to compare float values with == because you can be off by some very small amou...
Discriminated unions and other primitive types in F# uses structural equality by default, and provides a generated override for the .Equals method. The F# equality operator apparently differs from the C# one in that it uses the .Equals method even for reference types, but when F# discriminated unions are used from C#, the default operato...
from p.46 "Effective Java" Joshua Bloch. Item 9: ALways override hashCode when you override equals
Some class PhoneNumber overrides equals() and doesn't override hashCode()
"two instances are involved: one is used for insertion into the HashMap, and a second, equal, instance is used for (attempted) retrieval."
...
"... Even if the two ...
I'm having a strange problem comparing strings. I send a string to my server (as bytes using getBytes()) from the client. I've ensured that encoding is the same on the client and server by starting both of them with -Dfile.encoding=UTF-8.
I noticed the problem when I was trying to perform a valueOf on the string I receive from the clien...
Actually, I've found possible solution
//returns true
new BigDecimal ("5.50").doubleValue () == new BigDecimal("5.5").doubleValue ()
Of course, it can be improved with something like Math.abs (v1 - v2) < EPS to make the comparison more robust, but the question is whether this technique acceptable or is there a better solution?
If so...
When should one compare Strings as objects and when should one use their equals method? To make sure, I always use equals, but that doesn't seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use?
Thanks!
...
Consider the sample code given below:
Abstract Name
public abstract class Name {
private String name;
public Name(String name)
{
this.name=name;
}
public String toString()
{
return name;
}
public String getName() {
return name;
}
}
FirstName
public class FirstName extends Name {
FirstName(String name) {
super(...
I wonder how to compare two DateTime objects in .NET using DateTime methods Compare, CompareTo or Equals without comparing ticks.
I only need a tolerance level of milliseconds or seconds.
How can this be done?
...
I'm trying to write an equals method for objects that compares their fields, and if equal, then true:
public boolean equals(Ghost other){
if (this.x == other.x && this.y == other.y && this.direction==other.direction && this.color==other.color)
return true;
else
return...
I have an array of objects that I want to compare to a target object. I want to return the number of objects that exactly match the target object.
Here is my count method:
public int countMatchingGhosts(Ghost target) {
int count=0;
for (int i=0;i<ghosts.length;i++){
if (ghosts[i].equals(target));
...
I'm using Java 6. For simplicity, everything will be public.
Suppose I have this simple class.
public class A{
public String name;
public String data;
}
I want to put my class A objects into a HashMap. I will use the field name as the key, and the whole object as the value.
I will only search for an object in this map by na...