equals

.Contains() method not calling Overridden equals method

↘锁芯ラ 提交于 2019-11-29 01:47:36
问题 I am having an issue where I make an ArrayList of Foo objects, I override the equals method, and I cannot get the contains method to call the equals method. I have tried overriding equals and hashcode together, but it still doesn't work. I'm sure there is a logical explanation to why this is, but I cannot figure it out at the moment on my own lol. I just want a way to see if the list contains the specified id. Here's some code: import java.util.ArrayList; import java.util.List; public class

Java, how to compare Strings with String Arrays

可紊 提交于 2019-11-29 01:26:16
I have been searching here for some time but haven't been able to find the answer to it. I am basically required to use an array for this assignment from college. And then I am supposed to check that the input (which is also a String) matches whatever's stored within the String array. I know one can easily compare Strings by using the .equals() method. However, the same method is not working with the String array. I created the following example of code for the purpose of StackOverflow so you can use it to explain it to me, if you'd like. What am I doing wrong? import java.util.Scanner; class

Implementing GetHashCode correctly [duplicate]

旧时模样 提交于 2019-11-29 01:23:12
问题 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

Overriding Equals method in Structs

不问归期 提交于 2019-11-29 01:22:18
问题 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

Why should I not use equals with inheritance?

左心房为你撑大大i 提交于 2019-11-29 00:51:09
问题 When I read a Java book, author has said that, when designing a class, it's typically unsafe to use equals() with inheritance. For example: public final class Date { public boolean equals(Object o) { // some code here } } In the class above, we should put final , so other class cannot inherit from this. And my question is, why it is unsafe when allow another class inherit from this? 回答1: Because it's hard (impossible?) to make it right, especially the symmetric property. Say you have class

Java Set collection - override equals method

这一生的挚爱 提交于 2019-11-28 23:10:17
Is there any way to override the the equals method used by a Set datatype? I wrote a custom equals method for a class called Fee . Now I have a LnkedList of Fee and I want to ensure that there are no duplicated entries. Thus I am considering using a Set insted of a LinkedList , but the criteria for deciding if two fees are equal resides in the overriden equals method in the Fee class. If using a LinkedList , I will have to iterate over every list item and call the overriden equals method in the Fee class with the remaining entries as a parameter. Just reading this alone sounds like too much

MySQL - NULL safe NOT equal operator

会有一股神秘感。 提交于 2019-11-28 23:08:04
I am just curious. I know about NULL safe equal operator <=>, but is there some NULL safe NOT equal operator, or I have to always use something like that: (tab.id != 1 OR tab.id IS NULL) or someone prefers !(tab.id <=> 1) COALESCE(tab.id, 0) != 1 Can be used here if you like it. I goes through the parameters, and returns the first value that isn't NULL . In this case if it's NULL , it will compare 0 != 1 . Although it may use more signs, it's still easier to manage instead of being forced to always have opposite "booleans" as a solution in those cases. Read documentation for COALESCE() I found

JUnit assertEquals( ) fails for two objects

久未见 提交于 2019-11-28 23:05:05
I created a class and overridden the equals() method. When I use assertTrue(obj1.equals(obj2)) , it will pass the test; however, assertEquals(obj1, obj2) will fail the test. Could someone please tell the reason why? Jon Skeet My guess is that you haven't actually overridden equals - that you've overloaded it instead. Use the @Override annotation to find this sort of thing out at compile time. In other words, I suspect you've got: public boolean equals(MyClass other) where you should have: @Override // Force the compiler to check I'm really overriding something public boolean equals(Object

Compare Date object with a TimeStamp in Java

一世执手 提交于 2019-11-28 22:43:16
When I test this code: java.util.Date date = new java.util.Date(); java.util.Date stamp = new java.sql.Timestamp(date.getTime()); assertTrue(date.equals(stamp)); assertTrue(date.compareTo(stamp) == 0); assertTrue(stamp.compareTo(date) == 0); assertTrue(stamp.equals(date)); I´ll be expecting a true, true, true, false. Because of this: In the javadoc for java.sql.Timestamp, it states: Note: This type is a composite of a java.util.Date and a separate nanoseconds value. Only integral seconds are stored in the java.util.Date component. The fractional seconds - the nanos - are separate. The

Comparing two strings with “==”: when will it work?

我的未来我决定 提交于 2019-11-28 21:40:49
Say you have three strings, String s1 = "string one"; String s2 = new String("string one"); String s3 = "string one"; I know it is true that s1 == s2 is false , but I read somewhere that s1 == s3 is true . Is this correct? Why or why not? String literals are interned automatically. Hence s1 == s3 is true. Strings can either be created in the string constant pool or they can be created in the heap space. If you intern a string created in the heap, the string will be in the string constant pool. When you create a string literal (String s1 = "string one"), the string is created in the string