equals

equals() method for classes with bidirectional association

北城余情 提交于 2019-12-30 19:27:47
问题 I am trying to implement equals method for Java classes Book and Chapter in my application. Book has a set of Chapter s, while a Chapter has an associated Book . The bidirectional association is shown as below: class Book{ private String isbn; private String name; private Date publishDate; private Set<Chapter> chapters; ... public boolean equals(Object o){ if(o == this){ return true; } if (!(o instanceof Book)){ return false; } Book book = (Book)o; if( (this.isbn.equals(book.getIsbn()) ) &&

equals() method for classes with bidirectional association

为君一笑 提交于 2019-12-30 19:27:28
问题 I am trying to implement equals method for Java classes Book and Chapter in my application. Book has a set of Chapter s, while a Chapter has an associated Book . The bidirectional association is shown as below: class Book{ private String isbn; private String name; private Date publishDate; private Set<Chapter> chapters; ... public boolean equals(Object o){ if(o == this){ return true; } if (!(o instanceof Book)){ return false; } Book book = (Book)o; if( (this.isbn.equals(book.getIsbn()) ) &&

why listbox1.Items.Add use Equals Method of my Object?

倖福魔咒の 提交于 2019-12-30 16:07:12
问题 i'm scrutinizing Windows Forms ListBoxCollection Add Method, during the analyzing i found that the method "add" calls Equals method in base Object why this method do this action ? I Have used "Call Stack" and I have Find This Answer : We Call :System.Windows.Forms.ListBox.ObjectCollection.Add(object item) and it will Call : System.Windows.Forms.ListBox.ObjectCollection.AddInternal(object item) and it will Call : System.Windows.Forms.ListBox.NativeAdd(object item) and it will call : System

Is there a way to list all calls of equals() of a certain class using Eclipse?

我的梦境 提交于 2019-12-30 08:22:29
问题 I'm confronted with the following problem at the moment: I have a certain class in which the equals() - Method is overridden. However I'm not sure if it is ever being used (either in my or in one of my collegues projects). Is there a way to find out? When I search for references, well, it just gives me ALL references to the Object equals() - Method (which are quite a few). There surely must be an easier way than scan through all of them... Anyone got an idea? 回答1: You're asking Eclipse to

C# SortedSet<T> and equality

你。 提交于 2019-12-30 05:59:12
问题 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

Google App Engine, JDO, and equals/hashCode

拜拜、爱过 提交于 2019-12-30 03:09:29
问题 I've got an app in Google App Engine that was working fine. I realized that one on of my JDO-enhanced objects that I forgot to implement equals and hashCode (I need to use the object in a set). So I did. I didn't really do anything special in these implementations, in fact I just used Eclipse to generate them. Like so: @PrimaryKey @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Long id; @Persistent private String appleId; @Override public int hashCode() { final int prime =

C#: How does the static object.Equals check for equality?

ぐ巨炮叔叔 提交于 2019-12-30 02:42:12
问题 Say you have two different classes where each have their own implementation of Equals; which one is used? What if only one of them have one? Or none of them? Are any of the following lines equivalent? object .Equals( first, second ) first .Equals( second ) second .Equals( first ) I'm guessing that the first two might be equivalent, but I don't really have a clue. What does it really do? 回答1: Basically it does three things: Check for reference equality (return true if so) Check for reference

Java, Why it is implied that objects are equal if compareTo() returns 0?

大城市里の小女人 提交于 2019-12-29 06:38:30
问题 Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it

Java, Why it is implied that objects are equal if compareTo() returns 0?

女生的网名这么多〃 提交于 2019-12-29 06:38:08
问题 Let's have a class Person . Person has a name and height. Equals and hashCode() takes into account only name. Person is comparable (or we implement comparator for it, does not matter which one). Persons are compared by height. It seems reasonable to expect a situation where two different persons can have same height, but eg. TreeSet behaves like comapareTo()==0 means equals, not merely same size. To avoid this, comparison can secondarily look at something else if size is the same, but then it

Comparator and equals()

只愿长相守 提交于 2019-12-29 06:09:36
问题 Suppose I need TreeSet with elements sorted with some domain logic. By this logic it doesn't matter order of some elements that doesn't equal so compare method can return 0, but in this case I couldn't put them in TreeSet . So, question: what disadvantages I'll have from code like this: class Foo implements Comparable<Foo>{} new TreeSet<Foo>(new Comparator<Foo>(){ @Override public int compare(Foo o1, Foo o2) { int res = o1.compareTo(o2); if(res == 0 || !o1.equals(o2)){ return o1.hashCode() -