equals

Is it proper for equals() to depend only on an ID?

落花浮王杯 提交于 2019-12-03 04:30:24
Let's suppose I have class User : public class User { private Long id; private String name; private Integer age; private BigDecimal account; // other fields, getters and setters } Is it proper to override the equals method as follows? @Override public boolean equals(Object ob) { if (ob == null) { return false; } if (this == ob) { return true; } if (ob instanceof User) { User other = (User) ob; return this.id.equals(other.getId()); } return false; } It turns out that the uniqueness of an object is determined only by its ID. But in my application, id is always unique. It's provided at the

How to compare equality of lists of arrays with modern Java?

最后都变了- 提交于 2019-12-03 04:27:28
问题 I have two lists of arrays. How do I easily compare equality of these with Java 8 and its features , without using external libraries? I am looking for a "better" (higher-level, shorter, more efficient) solution than brute-force code like this (untested code, may contain typos etc, not the point of the question): boolean compare(List<String[]> list1, List<String[]> list2) { // tests for nulls etc omitted if(list1.size() != list2.size()) { return false; } for(i=0; i<list1.size(); ++i) { if(

Comparing doubles in Java gives odd results

淺唱寂寞╮ 提交于 2019-12-03 03:13:57
I really can'get my head around why the following happens: Double d = 0.0; System.out.println(d == 0); // is true System.out.println(d.equals(0)); // is false ?! This however works as expected: Double d = 0.0; System.out.println(d == 0.0); // true System.out.println(d.equals(0.0)); // true I'm positive that this is related to autoboxing in some way, but I really don't know why 0 would be boxed differently when the == operator is used and when .equals is called . Doesn't this implicitly violate the equals contract ? * It is reflexive: for any non-null reference value * x, x.equals(x) should

Best practice to choose fields for equals() implementation

≡放荡痞女 提交于 2019-12-03 01:58:55
When writing unit-tests, I often face the situation when equals() for some object in tests -- in assertEquals -- should work differently from how it works in actual environment. Take for example some interface ReportConfig . It has id and several other fields. Logically, one config equals to another one when their id s match. But when it comes to testing some specific implementation, say, XmlReportConfig , obviously I want to match all fields. One solution is not to use equals in tests and just iterate over the object properties or fields and compare them, but it doesn't seem like a good

Is there a way to auto-generate GetHashCode and Equals with ReSharper?

笑着哭i 提交于 2019-12-03 01:01:34
In eclipse, when I code in Java, there is a feature to auto-generate a basic, efficient, and bug free implementation of hashCode() and equals() without consuming brain power. Is there a similar feature either built-in in Visual Studio or in ReSharper ? svick Yes, Resharper can do that. With cursor inside your type, open the “Generate code” menu ( Alt + Ins depending on settings or Resharper -> Edit -> Generate Code ), and select “Equality members”: This opens a window where you can select which members are used for equality, along with some options about the generated code (e.g. should your

Check if bash variable equals 0 [duplicate]

孤街醉人 提交于 2019-12-03 00:55:53
问题 This question already has answers here : Comparing numbers in Bash (8 answers) Closed last year . I have a bash variable depth and I would like to test if it equals 0. In case yes, I want to stop executing of script. So far I have: zero=0; if [ $depth -eq $zero ]; then echo "false"; exit; fi Unfortunately, this leads to: [: -eq: unary operator expected (might be a bit inaccurate due to translation) Please, how can I modify my script to get it working? 回答1: Looks like your depth variable is

Integer value comparison

核能气质少年 提交于 2019-12-02 22:04:37
I'm a newbie Java coder and I just read a variable of an integer class can be described three different ways in the API. I have the following code: if (count.compareTo(0)) { System.out.println(out_table); count++; } This is inside a loop and just outputs out_table . My goal is to figure out how to see if the value in integer count > 0 . I realize the count.compare(0) is the correct way? or is it count.equals(0) ? I know the count == 0 is incorrect. Is this right? Is there a value comparison operator where its just count=0 ? Integers are autounboxed, so you can just do if (count > 0) { .... }

Using the equals() method with String and Object in Java

前提是你 提交于 2019-12-02 21:15:36
Object o1 = new Object(); Object o2 = new Object(); //o1=o2; System.out.println(o1.equals(o2)); It returns false . It can return true , if the comment is removed. Why isn't the same thing applicable to the String class? String s1=new String(); String s2=new String(); System.out.println(s1.equals(s2)); It returns true . Why? (because String uses interns or something else involved?) Because equals() for String compares the content, not the object itself. public boolean equals(Object anObject) Compares this string to the specified object. The result is true if and only if the argument is not null

Is it possible for 'this' keyword to equal null?

喜你入骨 提交于 2019-12-02 20:08:36
In an example, my professor has implemented Equals as follows: public class Person { private string dni; // ... public override bool Equals(object o) { if (o == null) return (this == null); else { return ((o is Person) && (this.dni == (o as Person).dni)); } } } I have no experience with C#, but as far as I know this cannot be null inside a member function (at least this is true in C++ and Java, the languages I know) so the if seems weird to me. Am I right or is there any component in c# I dont know of which makes the test this == null necesary? Eric Lippert I have no experience with C#, but as

How to implement “equals” method for generics using “instanceof”?

南楼画角 提交于 2019-12-02 19:21:23
I have a class that accepts a generic type , and I want to override the equals method in a non-awkward way (i.e. something that looks clean and has minimal amount of code, but for a very general use case). Right now I have something like this: public class SingularNode<T> { private T value; @SuppressWarnings("unchecked") @Override public boolean equals(Object other){ if(other instanceof SingularNode<?>){ if(((SingularNode<T>)other).value.equals(value)){ return true; } } return false; } } Which, I'm guessing, is pretty flawed - I'm doing a cast to SingularNode<T> on the other object, which