equals

Difference between matches and equalsIgnoreCase or equals in string class

你说的曾经没有我的故事 提交于 2019-11-30 05:03:29
matches : Will check if the complete string entered is equal to the value present in the string object. equalsIgnoreCase : Ignoring the case, it checks if the string entered is equal to the value present in the string object. equals : Case sensitive and it checks if the string entered is equal to the value present in the string object. This is what I know about the methods, present in String class. Are there any other differences(Am I missing any valuable differences)? If there are no differences, then why cant matches method be removed from String class, since the functionality it puts forth

Equals method for data class in kotlin

流过昼夜 提交于 2019-11-30 04:40:56
I have the following data class data class PuzzleBoard(val board: IntArray) { val dimension by lazy { Math.sqrt(board.size.toDouble()).toInt() } } I read that data classes in Kotlin get equals()/hashcode() method for free. I instantiated two objects. val board1 = PuzzleBoard(intArrayOf(1,2,3,4,5,6,7,8,0)) val board2 = PuzzleBoard(intArrayOf(1,2,3,4,5,6,7,8,0)) But still the following statements return false. board1 == board2 board1.equals(board2) In Kotlin data classes equality check, arrays, just like other classes, are compared using equals(...) , which compares the arrays references, not

Implementing GetHashCode correctly [duplicate]

萝らか妹 提交于 2019-11-30 04:37:18
This question already has an answer here: What is the best algorithm for overriding GetHashCode? 19 answers I'd like to hear from the community on how I should go about implementing GetHashCode (or override it) for my object. I understand I need to do so if I override the equals method. I have implemented it a fair amount of times, sometimes just calling the base method. I understand that my object should equal another instance of the object if it contains the same details (members). What is the best way to get a hash code from the class's members? Let's say your class looks like this: class

Overriding Equals method in Structs

删除回忆录丶 提交于 2019-11-30 04:10:17
I've looked for overriding guidelines for structs, but all I can find is for classes. At first I thought I wouldn't have to check to see if the passed object was null, as structs are value types and can't be null. But now that I come to think of it, as equals signature is public bool Equals(object obj) it seems there is nothing preventing the user of my struct to be trying to compare it with an arbitrary reference type. My second point concerns the casting I (think I) have to make before I compare my private fields in my struct. How am I supposed to cast the object to my struct's type? C#'s as

equals() and hashCode() contract in Java

喜欢而已 提交于 2019-11-30 04:03:42
问题 The SCJP 6 Study Guide from Bert Bates and Kathy Sierra states on page 554 (among other requirements) that x.hashCode() != y.hashCode() requires that x.equals(y) == false . But the Javadoc for Object doesn't mention such requirement explicitly. Quote: If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. Should I take what Javadoc says as a material implication, such as eq -> hc ? Then

Equality relations in Scala

a 夏天 提交于 2019-11-30 03:51:53
问题 I just stumbled on one of Tony Morris' blog-posts about Java and a fundamental problem with the language: that of defining a bespoke equality-relation for a collection. This is something that I think is a big deal and wondered whether there was some scala solution. The classic issue manifests itself in thinking about, say, a trade. Let's say I make two trades of +100 vodafone shares @150p. The two trades are equal, yes? Except they are not the same trade . In the case of a normal real-world

Updating Java HashMap key

只谈情不闲聊 提交于 2019-11-30 02:59:03
问题 I was just wondering, what would happen if key of a HashMap is mutable, test program below demonstrate that and I am unable to understand when both equals and hashCode methods returns true and same value, why does hashmap.containsKey return false . public class MutableKeyHashMap { public static void main(String []a){ HashMap<Mutable, String> map = new HashMap<Mutable, String>(); Mutable m1 = new Mutable(5); map.put(m1, "m1"); Mutable m2 = new Mutable(5); System.out.println(map.containsKey(m2)

Implementing equals method using compareTo

别等时光非礼了梦想. 提交于 2019-11-30 01:51:35
问题 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

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

无人久伴 提交于 2019-11-30 01:48:11
问题 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) . 回答1: Just put a '!' in front of

equals and hashCode: Is Objects.hash method broken?

故事扮演 提交于 2019-11-29 21:22:37
问题 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