equals

hashcode and equals contract vice versa

不打扰是莪最后的温柔 提交于 2019-12-07 02:04:28
I know the contract says "if two objects are equal then they should return the same hash code". That is so that those objects can be place in the same hash bucket and to improve hash code related collection functions know. Then again why it says "if two objects have same hash code those should not always be equals". I mean if it is true in the contract we should say "if two objects are equals they may return same hash code but which is not mandatory" I mean if it is true in the contract we should say "if two objects are equals they may return same hash code but which is not mandatory" No, it

What's the difference between `null == last` and `null eq last`, in Scala?

久未见 提交于 2019-12-07 00:06:04
问题 I see in the built-in class MessageQueue.scala of scala 2.7.7, around line 164, it's: def extractFirst(p: Any => Boolean): MessageQueueElement = { changeSize(-1) // assume size decreases by 1 val msg = if (null eq last) null else { ... } } I don't understand val msg = if (null eq last) null well, why it uses eq , but not null . If I write if (last==null) null , is it correct? Is there any difference? 回答1: When either side of the == is null or if the first operand of == evaluates to null, then

Java .equals() instanceof subclass? Why not call superclass equals instead of making it final?

只谈情不闲聊 提交于 2019-12-06 19:27:02
问题 It is stated in Object's .equals(Object) javadoc: It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. Almost everywhere in example code I see overridden .equals(Object) method which uses instanceof as one of the first tests, for example here: What issues / pitfalls must be considered when overriding equals and hashCode? public class Person { private String name; private int age; public boolean equals(Object obj) {

Why has Scala no type-safe equals method?

匆匆过客 提交于 2019-12-06 19:10:34
问题 Since the inventors highlight Scala 's type-safety I don't understand the absence of an equals method on objects (at least from case classes ) that allows to check the equality only on objects with same type. I'd wish a method === that implement this behavior per default. Of course its necessary for Java interoperability to have a method that works with Any type but in many cases I want to check only the equality between objects of same type. Why do I need it? For example I have two case

Jquery accordion not removingClass and not swapping its Expand text for Collapse

自作多情 提交于 2019-12-06 15:00:27
问题 Thanks to @ifaour for a lot of his help! This script includes: Jquery accordion using unordered lists. Active and Inactive states with toggling bullet arrow images Expand All / Collapse All that swaps its text. Equal height columns that expand and collapse when the accordion expands and collapses You can view a demo here http://jsbin.com/ucobo3/24/ (function($) { $.fn.equalHeights = function(minHeight, maxHeight) { tallest = (minHeight) ? minHeight : 0; this.each(function() { if($(this)

hashCode() 和 equals()

断了今生、忘了曾经 提交于 2019-12-06 08:34:09
第1部分 equals() 的作用 equals() 的作用是 用来判断两个对象是否相等 。 equals() 定义在JDK的Object.java中。通过判断两个对象的地址是否相等(即,是否是同一个对象)来区分它们是否相等。源码如下: public boolean equals(Object obj) { return ( this == obj); } 既然Object.java中定义了equals()方法,这就意味着所有的Java类都实现了equals()方法,所有的类都可以通过equals()去比较两个对象是否相等。 但是,我们已经说过,使用默认的“ equals() ”方法,等价于“ == ”方法。因此,我们通常会重写equals()方法:若两个对象的内容相等,则equals()方法返回true;否则,返回fasle。 下面根据“ 类是否覆盖equals()方法 ”,将它分为2类。 (01) 若某个类没有覆盖equals()方法,当它的通过equals()比较两个对象时,实际上是比较两个对象是不是同一个对象。这时,等价于通过“==”去比较这两个对象。 (02) 我们可以覆盖类的equals()方法,来让equals()通过其它方式比较两个对象是否相等。通常的做法是:若两个对象的内容相等,则equals()方法返回true;否则,返回fasle。 下面

Java equals()和hashCode()

懵懂的女人 提交于 2019-12-06 08:33:43
一、引言 Java技术面试的时候我们总会被问到这类的问题:重写equals()方法为什么一定要重写hashCode()方法?两个不相等的对象可以有相同的散列码吗?... 曾经对这些问题我也感到很困惑。 equals()和hasCode()方法是Object类中的两个基本方法。在实际的应用程序中这两个方法起到了很重要的作用,比如在集合中查找元素,我们经常会根据实际需要重写这两个方法。 下面就对equals()方法和hashCode()方法做一个详细的分析说明,希望对于有同样疑惑的人有些许帮助。 二、重写equals()方法 1、为什么要重写equals()方法 我们都知道比较两个对象引用是否相同用==运算符,且只有当两个引用都引用相同对象时,使用==运算符才会返回true。而比较相同类型的两个不同对象的内容是否相同,可以使用equals()方法。但是Object中的equals()方法只使用==运算符进行比较,其源码如下: public boolean equals(Object obj) { return (this == obj); } 如果我们使用Object中的equals()方法判断相同类型的两个不同对象是否相等,则只有当两个对象引用都引用同一对象时才被视为相等。那么就存在这样一个问题:如果我们要在集合中查找该对象, 在我们不重写equals()方法的情况下

== operator for primitive data types [duplicate]

余生长醉 提交于 2019-12-06 08:12:28
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How Does the toString(), ==, equals() object methods work differently or similarly on reference and primitive types? I am trying to understand the difference between == and equals to operator in Java. e.g. == will check if it is the same object while equals will compare the value of the object ... Then why do we use == for comparing primitive data types like int. Because if I have int i =7; //and int j = 6. They

How to implement equals for generic pairs?

谁都会走 提交于 2019-12-06 08:06:42
问题 Just for fun, I'm trying to implement a generic Pair class in Java. I'm having trouble with equals : public class Pair<A, B> { public final A _1; public final B _2; // ... unnecessary details left out ... public boolean equals(Pair<A, B> that) { return (_1.equals(that._1)) && (_2.equals(that._2)); } @Override public boolean equals(Object o) { return (o instanceof Pair<A, B>) && equals((Pair<A, B>) o); } } However, o instanceof Pair<A, B> does not seem to work. Why is that? Using (o instanceof

Comparing C# objects using Json

我与影子孤独终老i 提交于 2019-12-06 05:06:04
问题 I want to compare two objects without implementing the Equals() method. What are the downsides of comparing them in this way: 1. serilizing them with Json 2. comparing the results thanks! 回答1: What are the downsides of comparing them in this way Loss of speed. Transforming objects into JSON strings and then comparing them is much slower than doing a property by property equals. Implementing Equals() is always the best way to compare two objects for equality. 回答2: The downside is that you need