equals

Overriding the java equals() method - not working?

寵の児 提交于 2019-11-25 22:36:11
问题 I ran into an interesting (and very frustrating) issue with the equals() method today which caused what I thought to be a well tested class to crash and cause a bug that took me a very long time to track down. Just for completeness, I wasn\'t using an IDE or debugger - just good old fashioned text editor and System.out\'s. Time was very limited and it was a school project. Anyhow - I was developing a basic shopping cart which could contain an ArrayList of Book objects . In order to implement

Best implementation for hashCode method for a collection

烈酒焚心 提交于 2019-11-25 22:28:31
问题 How do we decide on the best implementation of hashCode() method for a collection (assuming that equals method has been overridden correctly) ? 回答1: The best implementation? That is a hard question because it depends on the usage pattern. A for nearly all cases reasonable good implementation was proposed in Josh Bloch 's Effective Java in Item 8 (second edition). The best thing is to look it up there because the author explains there why the approach is good. A short version Create a int

What issues should be considered when overriding equals and hashCode in Java?

拥有回忆 提交于 2019-11-25 22:10:14
问题 This post is a Community Wiki . Edit existing answers to improve this post. It is not currently accepting new answers. What issues / pitfalls must be considered when overriding equals and hashCode ? 回答1: The theory (for the language lawyers and the mathematically inclined): equals() (javadoc) must define an equivalence relation (it must be reflexive , symmetric , and transitive ). In addition, it must be consistent (if the objects are not modified, then it must keep returning the same value).

Compare two objects with .equals() and == operator

陌路散爱 提交于 2019-11-25 21:59:44
问题 I constructed a class with one String field. Then I created two objects and I have to compare them using == operator and .equals() too. Here\'s what I\'ve done: public class MyClass { String a; public MyClass(String ab) { a = ab; } public boolean equals(Object object2) { if(a == object2) { return true; } else return false; } public boolean equals2(Object object2) { if(a.equals(object2)) { return true; } else return false; } public static void main(String[] args) { MyClass object1 = new

What's the difference between “.equals” and “==”? [duplicate]

我与影子孤独终老i 提交于 2019-11-25 21:59:13
问题 This question already has an answer here: What is the difference between == and equals() in Java? 23 answers I switched lecturers today and he stated using a weird code to me. (He said it\'s better to use .equals and when I asked why, he answered \"because it is!\") So here\'s an example: if (o1.equals(o2)) { System.out.println(\"Both integer objects are the same\"); } Instead of what I\'m used to: if (o1 == o2) { System.out.println(\"Both integer objects are the same\"); } What\'s the

C# difference between == and Equals()

最后都变了- 提交于 2019-11-25 21:46:11
问题 I have a condition in a silverlight application that compares 2 strings, for some reason when I use == it returns false while .Equals() returns true . Here is the code: if (((ListBoxItem)lstBaseMenu.SelectedItem).Content.Equals(\"Energy Attack\")) { // Execute code } if (((ListBoxItem)lstBaseMenu.SelectedItem).Content == \"Energy Attack\") { // Execute code } Any reason as to why this is happening? 回答1: When == is used on an expression of type object , it'll resolve to System.Object