equals

Why BigDecimal(“5.50”) not equals to BigDecimal(“5.5”) and how to work around this issue?

好久不见. 提交于 2019-11-28 07:08:41
Actually, I've found possible solution //returns true new BigDecimal("5.50").doubleValue() == new BigDecimal("5.5").doubleValue() Of course, it can be improved with something like Math.abs (v1 - v2) < EPS to make the comparison more robust, but the question is whether this technique acceptable or is there a better solution? If someone knows why java designers decided to implement BigDecimal's equals in that way, it would be interesting to read. From the javadoc of BigDecimal equals public boolean equals(Object x) Compares this BigDecimal with the specified Object for equality. Unlike compareTo

toString(), equals(), and hashCode() in an interface

喜欢而已 提交于 2019-11-28 06:09:18
So, I have an interface with a bunch of methods that need to be implemented, the method names are irrelevant. The objects that implement this interface are often put into collections, and also have a special toString() format that I want them to use. So, I thought it would be convenient to put hashCode(), equals(), and toString() into the interface, to make sure that I remember to override the default method for these. But when I added these methods to the interface, the IDE/Compiler doesn't complain if I don't have those three methods implemented, even though I explicitly put them in the

How to compare two maps by their values

让人想犯罪 __ 提交于 2019-11-28 06:03:00
How to compare two maps by their values? I have two maps containing equal values and want to compare them by their values. Here is an example: Map a = new HashMap(); a.put("foo", "bar"+"bar"); a.put("zoo", "bar"+"bar"); Map b = new HashMap(); b.put(new String("foo"), "bar"+"bar"); b.put(new String("zoo"), "bar"+"bar"); System.out.println("equals: " + a.equals(b)); // obviously false How should I change the code to obtain a true? Jon Skeet Your attempts to construct different strings using concatenation will fail as it's being performed at compile-time. Both of those maps have a single pair;

Equals(item, null) or item == null

流过昼夜 提交于 2019-11-28 05:15:53
Is code that uses the static Object.Equals to check for null more robust than code that uses the == operator or regular Object.Equals ? Aren't the latter two vulnerable to being overridden in such a way that checking for null doesn't work as expected (e.g. returning false when the compared value is null)? In other words, is this: if (Equals(item, null)) { /* Do Something */ } more robust than this: if (item == null) { /* Do Something */ } I personally find the latter syntax easier to read. Should it be avoided when writing code that will handle objects outside the author's control (e.g.

Operator Overloading with Interface-Based Programming in C#

北城以北 提交于 2019-11-28 03:53:24
Background I am using interface-based programming on a current project and have run into a problem when overloading operators (specifically the Equality and Inequality operators). Assumptions I'm using C# 3.0, .NET 3.5 and Visual Studio 2008 UPDATE - The Following Assumption was False! Requiring all comparisons to use Equals rather than operator== is not a viable solution, especially when passing your types to libraries (such as Collections). The reason I was concerned about requiring Equals to be used rather than operator== is that I could not find anywhere in the .NET guidelines that it

Why is myString.equals(“aString”); different from “aString”.equals(myString);?

为君一笑 提交于 2019-11-28 03:43:41
问题 I heard several times that in using boolean equals(Object o) to compare Strings , it's better to put the constant on the left side of the function as in the following: Bad: myString.equals("aString"); Good: "aString".equals(myString); Why is this? 回答1: Because if myString is null you get an exception. You know "aString" will never be null, so you can avoid that problem. Often you'll see libraries that use nullSafeEquals(myString,"aString"); everywhere to avoid exactly that (since most times

Overriding equals() & hashCode() in sub classes … considering super fields

二次信任 提交于 2019-11-28 03:26:53
Is there a specific rule on how Overriding equals() & hashCode() in sub classes considering super fields ?? knowing that there is many parameters : super fields are private/public , with/without getter ... For instance, Netbeans generated equals() & hashCode() will not consider the super fields ... and new HomoSapiens("M", "80", "1.80", "Cammeron", "VeryHot").equals( new HomoSapiens("F", "50", "1.50", "Cammeron", "VeryHot")) will return true :( public class Hominidae { public String gender; public String weight; public String height; public Hominidae(String gender, String weight, String height

What are not 2 Long variables equal with == operator to compare in Java?

天涯浪子 提交于 2019-11-28 02:35:58
问题 I got a very strange problem when I'm trying to compare 2 Long variables, they always show false and I can be sure they have the same number value by debugging in Eclipse: if (user.getId() == admin.getId()) { return true; // Always enter here } else { return false; } Both of above 2 return values are object-type Long, which confused me. And to verify that I wrote a main method like this: Long id1 = 123L; Long id2 = 123L; System.out.println(id1 == id2); It prints true. So can somebody give me

equals and hashCode of these entities (Spring MVC + Hibernate)

痞子三分冷 提交于 2019-11-28 02:20:31
Someone can please suggest me how I can do equals and hashCode methods of these entities? This is a many-to-many relationship between a Gara (Contest) and Agenzia (Agency): One contest has many Agency, one Agency can be in more Contest. I tried some implementations but or I get Stackoverflow error, or, when I'm updating a Gara (Contest), I can't update the set of Agenzie (Agencies) because i get this error: org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [com.myApp.model.GaraAgenzia#com.mmyApp.model

Why does '$true -eq “string”' returns $true? [duplicate]

感情迁移 提交于 2019-11-28 01:29:57
问题 This question already has answers here : Why is $false -eq “” true? (2 answers) Closed 5 years ago . In powerShell you compare a boolean with a string with the "-eq" operator it will always return the same boolean as I used to compare. E.g. $shouldBeFalse = $true -eq "hello" $shouldBeTrue = $false -eq "hello" The variable $shouldBeFalse is $true. The variable $shouldBeTrue is $false. I had to use the "Equals" method: $shouldBeFalse = $true.Equals("hello") In this case $shouldBeFalse is $false