equals

Why do we have to override the equals() method in Java?

让人想犯罪 __ 提交于 2019-11-26 08:16:49
问题 I have some confusion about the reason that we override the .equals method. For example: Test test1 = new Test(3); Test test2 = new Test(3); //The if comparison does the same thing that the overridden `.equals()` method does. if(test1.equals(test2)){ System.out.println(\"test1 and test2 are true in .equals()\"); } // Override .equals method. public boolean equals(Object object) { if(object instanceof Test && ((Test)object).getValue() == this.t) { return true; } else { return false; } } I do

Setting equal heights for div's with jQuery

廉价感情. 提交于 2019-11-26 07:23:22
问题 I want to set equal height for divs with jQuery. All the divs may have different amount of content and different default height. Here is a sample of my html layout: <div class=\"container\"> <div class=\"column\">This is<br />the highest<br />column</div> <div class=\"column\">One line</div> <div class=\"column\">Two<br />lines</div> </div> <div class=\"container\"> <div class=\"column\">One line</div> <div class=\"column\">Two<br>lines</div> <div class=\"column\">One line</div> </div> I\'m

Understanding the workings of equals and hashCode in a HashMap

跟風遠走 提交于 2019-11-26 06:59:18
I have this test code: import java.util.*; class MapEQ { public static void main(String[] args) { Map<ToDos, String> m = new HashMap<ToDos, String>(); ToDos t1 = new ToDos("Monday"); ToDos t2 = new ToDos("Monday"); ToDos t3 = new ToDos("Tuesday"); m.put(t1, "doLaundry"); m.put(t2, "payBills"); m.put(t3, "cleanAttic"); System.out.println(m.size()); } } class ToDos{ String day; ToDos(String d) { day = d; } public boolean equals(Object o) { return ((ToDos)o).day == this.day; } // public int hashCode() { return 9; } } When // public int hashCode() { return 9; } is uncommented m.size() returns 2,

Hashcode and Equals for Hashset [duplicate]

一个人想着一个人 提交于 2019-11-26 06:33:10
问题 This question already has an answer here: When does HashSet 'add' method calls equals? [duplicate] 4 answers Please clarify my doubt in Hashset. Consider the following code, class Person { String name; Person(String n) { name=n; } public String getName() { return name; } @Override public boolean equals(Object arg0) { System.out.println(\"in equals\"); Person obj=(Person)arg0; System.out.println(\"1st \"+getName()); System.out.println(\"2nd \"+obj.getName()); if(this.getName().equals(obj

What&#39;s the difference between IEquatable and just overriding Object.Equals()?

人走茶凉 提交于 2019-11-26 06:13:40
问题 I want my Food class to be able to test whenever it is equal to another instance of Food . I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquatable<Food> or just override Object.Equals() ? From MSDN: This method determines equality by using the default equality comparer, as defined by the object\'s implementation of the IEquatable.Equals method for T (the type of values in the list). So my next question is: which functions/classes of the

Apache Commons equals/hashCode builder [closed]

痞子三分冷 提交于 2019-11-26 06:11:09
问题 I\'m curious to know, what people here think about using org.apache.commons.lang.builder EqualsBuilder / HashCodeBuilder for implementing the equals / hashCode ? Would it be a better practice than writing your own? Does it play well with Hibernate? What\'s your opinion? 回答1: The commons/lang builders are great and I have been using them for years without noticeable performance overhead (with and without hibernate). But as Alain writes, the Guava way is even nicer: Here's a sample Bean: public

When does HashSet &#39;add&#39; method calls equals? [duplicate]

帅比萌擦擦* 提交于 2019-11-26 06:03:42
问题 This question already has answers here : What issues should be considered when overriding equals and hashCode in Java? (11 answers) Closed last year . I did this test in a HashSet comparision and equals is not being called I would like to consider equals when farAway=false (A function to check two point distances) Full compilable code, you could test it, and tells why equals is not being called in this example. public class TestClass{ static class Posicion { private int x; private int y;

How to ensure hashCode() is consistent with equals()?

守給你的承諾、 提交于 2019-11-26 06:02:24
问题 When overriding the equals() function of java.lang.Object, the javadocs suggest that, it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. The hashCode() method must return a unique integer for each object (this is easy to do when comparing objects based on memory location, simply return the unique integer address of the object)

How to quickly check if two data transfer objects have equal properties in C#?

て烟熏妆下的殇ゞ 提交于 2019-11-26 05:55:29
问题 I have these data transfer objects: public class Report { public int Id { get; set; } public int ProjectId { get; set; } //and so on for many, many properties. } I don\'t want to write public bool areEqual(Report a, Report b) { if (a.Id != b.Id) return false; if (a.ProjectId != b.ProjectId) return false; //Repeat ad nauseum return true; } Is there a faster way to test if two object with only properties have the same values (something that doesn\'t require one line of code or one logical

BigDecimal equals() versus compareTo()

天大地大妈咪最大 提交于 2019-11-26 05:21:52
问题 Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BigDecimal x = new BigDecimal(\"1\"); BigDecimal y = new BigDecimal(\"1.00\"); System.out.println(x.equals(y)); System.out.println(x.compareTo(y) == 0 ? \"true\": \"false\"); } } You can (consciously) say that x is equal to y (not object reference), but when you run the program,