equals

Implementing equals method using compareTo

徘徊边缘 提交于 2019-11-30 18:03:27
General question: When implementing an override of the default equals method in Java, what concerns should I have about simply utilizing an already implemented compareTo method vs writing independent logic into the equals method? I noticed someone mention in another question that foo.equals((String)null) returns false whereas String.compareTo((String)null) throws a NullPointerException . What makes these inconsistent results ideal functionality? Sample equals method: @Override public boolean equals(Object obj) { if (obj != null && obj instanceof MyClass) { MyClass msg = (MyClass)obj; return

How can I express that two values are not equal to eachother?

…衆ロ難τιáo~ 提交于 2019-11-30 17:56:03
Is there a method similar to equals() that expresses "not equal to"? An example of what I am trying to accomplish is below: if (secondaryPassword.equals(initialPassword)) { JOptionPane.showMessageDialog(null, "You've successfully completed the program."); } else { secondaryPassword = JOptionPane.showInputDialog(null, "Your passwords do not match. Please enter you password again."); } I am trying to find something that will not require me to use if ( a != c) . Just put a '!' in front of the boolean expression "Not equals" can be expressed with the "not" operator ! and the standard .equals . if

C# SortedSet<T> and equality

こ雲淡風輕ζ 提交于 2019-11-30 17:53:33
I am a bit puzzled about the behaviour of SortedSet, see following example: public class Blah { public double Value { get; private set; } public Blah(double value) { Value = value; } } public class BlahComparer : Comparer<Blah> { public override int Compare(Blah x, Blah y) { return Comparer<double>.Default.Compare(x.Value, y.Value); } } public static void main() { var blahs = new List<Blah> {new Blah(1), new Blah(2), new Blah(3), new Blah(2)} //contains all 4 entries var set = new HashSet<Blah>(blahs); //contains only Blah(1), Blah(2), Blah(3) var sortedset = new SortedSet<Blah>(blahs, new

equals and hashCode: Is Objects.hash method broken?

元气小坏坏 提交于 2019-11-30 15:36:16
I am using Java 7, and I have the following class below. I implemented equals and hashCode correctly, but the problem is that equals returns false in the main method below yet hashCode returns the same hash code for both objects. Can I get more sets of eyes to look at this class to see if I'm doing anything wrong here? UPDATE: I replaced the line on which I call the Objects.hash method with my own hash function: chamorro.hashCode() + english.hashCode() + notes.hashCode() . It returns a different hash code, which is what hashCode is supposed to do when two objects are different. Is the Objects

T[].Contains for struct and class behaving differently

可紊 提交于 2019-11-30 14:48:50
问题 This is a followup question to this: List<T>.Contains and T[].Contains behaving differently T[].Contains is behaving differently when T is class and struct. Suppose I have this struct : public struct Animal : IEquatable<Animal> { public string Name { get; set; } public bool Equals(Animal other) //<- he is the man { return Name == other.Name; } public override bool Equals(object obj) { return Equals((Animal)obj); } public override int GetHashCode() { return Name == null ? 0 : Name.GetHashCode(

Is there a __equals method in PHP like there is in Java?

三世轮回 提交于 2019-11-30 14:28:19
问题 Is there a pattern or magic method you can use in PHP to define when to compare two instances of a class? For example, in Java I could easily override the equals method and create a custom way of checking and compare two instances. 回答1: In a word? No. There is no __equals magic method. There is a complete list of the magic methods in the manual. You can do $myObject1 == $myObject2 which will consider them equal if they have the same attributes and values, and are instances of the same class.

Hibernate: When is it necessary to implement equals() and hashCode(), and if so, how?

南笙酒味 提交于 2019-11-30 13:55:48
Based on various bad experiences my rule of thumb as a Java programmer is to only implement equals() and hashCode() on immutable objects, where two instances of the object really are interchangeable. Basically I want to avoid situations like the HashMap key problem in that link, or like the following: Get a thing with a certain identity. Modify it. Add it to a set. (later) Get another thing with the same identity. Modify it. Add it to the same set. Fail to notice that this add doesn't actually happen, since the set thinks the thing is already there. Do something with the things in the set.

Why can't I “static import” an “equals” method in Java?

删除回忆录丶 提交于 2019-11-30 13:38:28
问题 I like using this method here: org.apache.commons.lang.ObjectUtils.equals(Object object1, Object object2) The only drawback (compared to Google Guava, for instance), is that I cannot static import the method. I.e. this is useless: import static org.apache.commons.lang.ObjectUtils.equals; ... as my Eclipse compiler will not correctly link that method when writing equals(obj1, obj2); The error is: The method equals(Object) in the type Object is not applicable for the arguments (..., ...) Why is

Hibernate equals and proxy

删除回忆录丶 提交于 2019-11-30 13:15:52
问题 I have one BaseEntity which abstracts id and version property. this class also implements hashcode and equals based on PK (id) property. BaseEntity{ Long id; Long version; public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; BaseEntity other = (BaseEntity) obj; if

How do I check if an object is equal to a new object of the same class?

谁都会走 提交于 2019-11-30 13:08:11
If I have a object like: public class Person { public int id {get;set;} public string name {get;set;} } And I want the behavior: Person a = new Person(); Person b = new Person(); a == b; and that a == b returns true, do I have to override the Object.Equals() method? or is there some other way of doing it without overriding the Equals method? EDIT I want to compare data, as I want to know if a external method that I call returns a new object or a object with different data than a new object Abbas There are a couple of ways you can do this. By default Equals() and == check for reference equality