I have:
List<SlaveEntityDTO> result = Jsoner.JsonToSlaveEntityDTO(json);
List<SlaveEntityDTO> result1 = entitiesDTOList;
The result and result1 has the same values for their fields:
When I run Assert.assertEquals(result, result1); I am getting the following message:
java.lang.AssertionError:
Expected :[core.dto.SlaveEntityDTO#6be46e8f, core.dto.SlaveEntityDTO#3567135c]
Actual :[core.dto.SlaveEntityDTO#327471b5, core.dto.SlaveEntityDTO#4157f54e]
So how can I compare the values of the fields inside result and result1, instead of comparing if an object is that object?
The SlaveEntityDTO is like this:
public class SlaveEntityDTO extends BaseEntityDTO<SlaveEntity> {
private String ip;
private String macAddress;
private String status;
private List<PositionEntity> positions;
#Override
public SlaveEntity convertToEntity() {
return new ModelMapper().map(this, SlaveEntity.class);
}
}
And the BaseEntityDTO is like this:
public abstract class BaseEntityDTO<T> implements Serializable{
private long id;
public abstract T convertToEntity();
}
Your test looks fine. The List interface defines the behavior of its equals, and your debugger shows that ArrayList is being used. ArrayList is a good guy, so we can assume that its implementation of equals is legit.
Thus, we can conclude that your SlaveEntityDTO class either does not override Object#equals(Object) or that it does so in a way that you aren't accounting for (which possibly means that it is implementing it incorrectly).
You can fix this by Overriding equals in BaseEntityDTO. This will give basic behavior of equals to every subclass.
#Override
public boolean equals(Object o) {
if (this == o) return true; // literally the same object.
if (o == null || getClass() != o.getClass()) return false; // Not correct type.
BaseEntityDTO that = (BaseEntityDTO) o;
return this.id == null ? that.id == null : this.id.equals(that.id);
}
And don't forget: hashCode() MUST match the implementation of equals!
#Override
public int hashCode() {
return id == null ? 0 : id.hashCode();
}
When I run Assert.assertEquals(result, result1); I am getting the
following message:
java.lang.AssertionError: Expected
:[core.dto.SlaveEntityDTO#6be46e8f, core.dto.SlaveEntityDTO#3567135c]
Actual :[core.dto.SlaveEntityDTO#327471b5,
core.dto.SlaveEntityDTO#4157f54e]
As you said, you are getting the error because you are comparing the objects and not the content of the two objects.
One way of doing it would be to convert both the JSON objects to Strings and then compare the two Strings but remember that order in JSON is not fixed and it might happen that your result object has the elements in order {2, 1, 3} but the source object has it in the order {1, 2, 3}.
I think you should try creating Sets out of your source elements and result elements and then compare both the Sets based on their sizes and also elements in it to assert whether the two objects are equal or not.
You can read this post here to know more about JSON comparison:
Related
Hello I probably oversaw something, but here it goes.
I have a TreeSet<CustomObject> and I do not want to have duplicates in the Set. My CustomObject class looks like this.
class CustomObject implements Comparable<CustomObject> {
String product;
String ean;
public CustomObject(String ean){
this.ean = ean;
// product is getting set via setter and can be null
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CustomObject that = (CustomObject) o;
return ean.equals(that.ean);
}
#Override
public int hashCode() {
return ean.hashCode();
}
#Override
public int compareTo(CustomObject another) {
if(equals(another)) return 0;
if(product != null && another.product == null) return -1;
if(product == null) return 1;
return product.compareToIgnoreCase(another.product);
}
}
Now I have a add Function for new objects.
private final TreeSet<CustomObject> productCatalog;
public void addObject(SomeData tag) {
CustomObject p = new CustomObject(tag.getEan());
if (productCatalog.contains(p)) { // <-- This checks only one entry of the Set.
for (CustomObject temp : productCatalog) {
if (temp.equals(p)) {
p = temp; // I do stuff with that later which is irrelevent here
}
}
} else {
productCatalog.add(p);
}
}
The method productCatalog.contains(p) calls the compareTo method from the Comparable Interface and does the comparing. The issue here is that it literally only checks I think the last object? in the set. So what happens is that only one unique CustomObject entry is present.
This is the scenario when I follow it with the debugger:
productCatalog.contains(p)
calls compareTo
calls equals to check if ean.equals(that.ean)
returns once true, but every other time false. Since it only checks the last object
How can I have it to check not only one object in step 4, but all the present objects in the Set. What am I missing?
Thx!
EDIT: These are some sample data. For the simplicity SomeData tag is basically a String.
First run:
addObject("Ean1") // success added
addObject("Ean2") // success added
addObject("Ean3") // success added
addObject("Ean4") // success added
Everything gets added into the TreeSet.
Second run:
addObject("Ean1") // failed already in the map
addObject("Ean2") // failed already in the map
addObject("Ean3") // failed already in the map
addObject("Ean5") // success added
addObject("Ean4") // success added
addObject("Ean4") // success added
For testing purpose I manually set product names depending on the String ean.
public CustomObject(String ean){
this.ean = ean;
switch(ean){
case "Ean1": product = "TestProduct"; break;
case "Ean2": product = "ProductTest";break;
case "Ean3": product = "Product";break;
}
The TreeSet acts as a cache.
Edit2: This is how I solved it.
for (CustomObject temp : productCatalog) {
if (temp.equals(p)) {
p = temp; // I do stuff with that later which is irrelevent here
}
}
I removed the if statement with the contains method since that would always return ยด1or-1in my special case. Now I simply iterate over the Set to correctly use theequals` method since the TreeSet uses compareTo() for checking every element in the Set.
The Java Docs state the following
Note that the ordering maintained by a set (whether or not an explicit
comparator is provided) must be consistent with equals if it is to
correctly implement the Set interface. (See Comparable or Comparator
for a precise definition of consistent with equals.) This is so
because the Set interface is defined in terms of the equals operation,
but a TreeSet instance performs all element comparisons using its
compareTo (or compare) method, so two elements that are deemed equal
by this method are, from the standpoint of the set, equal. The
behavior of a set is well-defined even if its ordering is inconsistent
with equals; it just fails to obey the general contract of the Set
interface.
The main problem:
compareTo does return 1 if both product and other.product are null. This is wrong because they are actually equal. You probably forgot to set product names for the higher ean values, like "Ean4" and "Ean5".
Old answer:
Your implementations of equals and compareTo do not fit together.
equals works on the ean and compareTo on the product. This only works if you implicitly assume that equal ean imply equal product. If this is not true in your test cases, the result will be wrong.
In either case, it is no good implementation because this can lead to a < b, b < c but a "equals" c.
I constructed a class with one String field. Then I created two objects and I have to compare them using == operator and .equals() too. Here's what I've done:
public class MyClass {
String a;
public MyClass(String ab) {
a = ab;
}
public boolean equals(Object object2) {
if(a == object2) {
return true;
}
else return false;
}
public boolean equals2(Object object2) {
if(a.equals(object2)) {
return true;
}
else return false;
}
public static void main(String[] args) {
MyClass object1 = new MyClass("test");
MyClass object2 = new MyClass("test");
object1.equals(object2);
System.out.println(object1.equals(object2));
object1.equals2(object2);
System.out.println(object1.equals2(object2));
}
}
After compile it shows two times false as a result. Why is it false if the two objects have the same fields - "test"?
== compares object references, it checks to see if the two operands point to the same object (not equivalent objects, the same object).
If you want to compare strings (to see if they contain the same characters), you need to compare the strings using equals.
In your case, if two instances of MyClass really are considered equal if the strings match, then:
public boolean equals(Object object2) {
return object2 instanceof MyClass && a.equals(((MyClass)object2).a);
}
...but usually if you are defining a class, there's more to equivalency than the equivalency of a single field (a in this case).
Side note: If you override equals, you almost always need to override hashCode. As it says in the equals JavaDoc:
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
You should override equals
public boolean equals (Object obj) {
if (this==obj) return true;
if (this == null) return false;
if (this.getClass() != obj.getClass()) return false;
// Class name is Employ & have lastname
Employe emp = (Employee) obj ;
return this.lastname.equals(emp.getlastname());
}
The best way to compare 2 objects is by converting them into json strings and compare the strings, its the easiest solution when dealing with complicated nested objects, fields and/or objects that contain arrays.
sample:
import com.google.gson.Gson;
Object a = // ...;
Object b = //...;
String objectString1 = new Gson().toJson(a);
String objectString2 = new Gson().toJson(b);
if(objectString1.equals(objectString2)){
//do this
}
The overwrite function equals() is wrong.
The object "a" is an instance of the String class and "object2" is an instance of the MyClass class. They are different classes, so the answer is "false".
It looks like equals2 is just calling equals, so it will give the same results.
Your equals2() method always will return the same as equals() !!
Your code with my comments:
public boolean equals2(Object object2) { // equals2 method
if(a.equals(object2)) { // if equals() method returns true
return true; // return true
}
else return false; // if equals() method returns false, also return false
}
The "==" operator returns true only if the two references pointing to the same object in memory. The equals() method on the other hand returns true based on the contents of the object.
Example:
String personalLoan = new String("cheap personal loans");
String homeLoan = new String("cheap personal loans");
//since two strings are different object result should be false
boolean result = personalLoan == homeLoan;
System.out.println("Comparing two strings with == operator: " + result);
//since strings contains same content , equals() should return true
result = personalLoan.equals(homeLoan);
System.out.println("Comparing two Strings with same content using equals method: " + result);
homeLoan = personalLoan;
//since both homeLoan and personalLoan reference variable are pointing to same object
//"==" should return true
result = (personalLoan == homeLoan);
System.out.println("Comparing two reference pointing to same String with == operator: " + result);
Output:
Comparing two strings with == operator: false
Comparing two Strings with same content using equals method: true
Comparing two references pointing to same String with == operator: true
You can also get more details from the link: http://javarevisited.blogspot.in/2012/12/difference-between-equals-method-and-equality-operator-java.html?m=1
Statements a == object2 and a.equals(object2) both will always return false because a is a string while object2 is an instance of MyClass
Your implementation must like:
public boolean equals2(Object object2) {
if(a.equals(object2.a)) {
return true;
}
else return false;
}
With this implementation your both methods would work.
If you dont need to customize the default toString() function, another way is to override toString() method, which returns all attributes to be compared. then compare toString() output of two objects. I generated toString() method using IntelliJ IDEA IDE, which includes class name in the string.
public class Greeting {
private String greeting;
#Override
public boolean equals(Object obj) {
if (this == obj) return true;
return this.toString().equals(obj.toString());
}
#Override
public String toString() {
return "Greeting{" +
"greeting='" + greeting + '\'' +
'}';
}
}
Your class might implement the Comparable interface to achieve the same functionality. Your class should implement the compareTo() method declared in the interface.
public class MyClass implements Comparable<MyClass>{
String a;
public MyClass(String ab){
a = ab;
}
// returns an int not a boolean
public int compareTo(MyClass someMyClass){
/* The String class implements a compareTo method, returning a 0
if the two strings are identical, instead of a boolean.
Since 'a' is a string, it has the compareTo method which we call
in MyClass's compareTo method.
*/
return this.a.compareTo(someMyClass.a);
}
public static void main(String[] args){
MyClass object1 = new MyClass("test");
MyClass object2 = new MyClass("test");
if(object1.compareTo(object2) == 0){
System.out.println("true");
}
else{
System.out.println("false");
}
}
}
the return type of object.equals is already boolean.
there's no need to wrap it in a method with branches. so if you want to compare 2 objects simply compare them:
boolean b = objectA.equals(objectB);
b is already either true or false.
When we use == , the Reference of object is compared not the actual objects. We need to override equals method to compare Java Objects.
Some additional information C++ has operator over loading & Java does not provide operator over loading.
Also other possibilities in java are implement Compare Interface .which defines a compareTo method.
Comparator interface is also used compare two objects
Here the output will be false , false beacuse in first sopln statement you are trying to compare a string type varible of Myclass type to the other MyClass type and it will allow because of both are Object type and you have used "==" oprerator which will check the reference variable value holding the actual memory not the actual contnets inside the memory .
In the second sopln also it is the same as you are again calling a.equals(object2) where a is a varible inside object1 . Do let me know your findings on this .
In short, == compares two POINTERS.
If the two pointers are equal, then they both point to same object in memory (which, obviously has the same value as itself).
However, .equals will compare the VALUES of whatever is pointed to, returning true iff they both evaluate to the same value.
Thus, two separate strings (i.e., at different addresses in memory) are always != but are .equal iff they contain the same (null-terminated) sequence of chars.
IN the below code you are calling the overriden method .equals().
public boolean equals2(Object object2) {
if(a.equals(object2)) { // here you are calling the overriden method, that is why you getting false 2 times.
return true;
}
else return false;
}
I made my own class with an overridden equals method which just checks, if the names (attributes in the class) are equal. Now I store some instances of that class in a HashSet so that there are no instances with the same names in the HashSet.
My Question: How is it possible to check if the HashSet contains such an object. .contains() wont work in that case, because it works with the .equals() method. I want to check if it is really the same object.
edit:
package testprogram;
import java.util.HashSet;
import java.util.Set;
public class Example {
private static final Set<Example> set = new HashSet<Example>();
private final String name;
private int example;
public Example(String name, int example) {
this.name = name;
this.example = example;
set.add(this);
}
public boolean isThisInList() {
return set.contains(this);
//will return true if this is just equal to any instance in the list
//but it should not
//it should return true if the object is really in the list
}
public boolean remove() {
return set.remove(this);
}
//Override equals and hashCode
}
Sorry, my english skills are not very well. Please feel free to ask again if you don't understand what I mean.
In your situation, the only way to tell if a particular instance of an object is contained in the HashSet, is to iterate the contents of the HashSet, and compare the object identities ( using the == operator instead of the equals() method).
Something like:
boolean isObjectInSet(Object object, Set<? extends Object> set) {
boolean result = false;
for(Object o : set) {
if(o == object) {
result = true;
break;
}
}
return result;
}
The way to check if objects are the same object is by comparing them with == to see that the object references are equal.
Kind Greetings,
Frank
You will have to override the hashCode method also.
try this..
Considering only one property 'name' of your Objects to maintain uniqueness.
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (name == null ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
User other = (User) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}
I made my own class with an overridden equals method which just checks, if the names (attributes in the class) are equal.
This breaks the contract of .equals, and you must never do it no matter how convenient it seems.
Instead, if you want to index and look up elements by a certain attribute such as the name, use a HashMap<Name, YourType> to find them. Alternatively, use a TreeSet and pass it a Comparator that compares the name only. You can then remove the incorrect equals method.
There are then three ways if you want to find objects by reference equality:
Your objects have no inherent or useful notion of equality.
Don't implement equals. Leave it to its default. You can then use a HashSet to look for reference equality, and a HashMap or TreeSet to index them by any specific attributes.
Your objects do have a useful, universal notion of equality, but you want to find equivalent instances efficiently anyways.
This is almost never the case. However, you can use e.g. an Apache IdentityMap.
You don't care about efficiency.
Use a for loop and == every element.
HashSet contains uses the equals method to determine if the object is contained - and duplicates are not kept within the HashSet.
Assuming your equals and hashcode are only using a name field...
HashSet<MyObject> objectSet = new HashSet<MyObject>();
MyObject name1Object = new MyObject("name1");
objectSet.add(new MyObject("name1"));
objectSet.add(name1Object);
objectSet.add(new MyObject("name2"));
//HashSet now contains 2 objects, name1Object and the new name2 object
//HashSets do not hold duplicate objects (name1Object and the new object with name1 would be considered duplicates)
objectSet.contains(new MyObject("name1")) // returns true
objectSet.contains(name1Object) // returns true
objectSet.contains(new MyObject("name2")) // returns true
objectSet.contains(new MyObject("name3")) // returns false
If you wanted to check if the object in the HashSet is the exact object you are comparing you would have to pull it out and compare it directly using ==
for (MyObject o : objectSet)
{
if (o == name1Object)
{
return true;
}
}
If you do this alot for specific objects it might be easier to use a HashMap so you don't have to iterate through the list to grab a specific named Object. May be worth looking into for you because then you could do something like this:
(objectMap.get("name") == myNameObject) // with a HashMap<String, MyNameObject> where "name" is the key string.
I have a School class:
public class School {
private String name;
private int id;
private boolean isOpen;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isOpen() {
return isOpen;
}
public void setOpen(boolean isOpen) {
this.isOpen = isOpen;
}
}
Then I created two instances of School, and compare the equality of the two instances:
public static void main(String[] args) {
//school1
School school1 = new School();
school1.setId(1);
school1.setName("schoolOne");
//school2
School school2 = new School();
school2.setId(1);
school2.setName("schoolOne");
//result is false , why?
System.out.println("school1 == school2 ? " + school1.equals(school2));
}
Even though I set the same id and name to school1 & school2 instances, but school1.equals(school2) returns false, why?
You have to override the equals(Object) method:
Place this in your School class:
#Override
public boolean equals(Object other) {
if (other == this) return true;
if (other == null || !(other instanceof School)) return false;
School school = (School) other;
if (school.id != this.id) return false;
if (!(school.name.equals(this.name))) return false;
if (school.isOpen != this.isOpen) return false;
if (!(school.hashCode().equals(this.hashCode()))) return false;
return true;
}
If you are going to this, it is also wise to override the hashCode() method as well.
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) id;
result = prime * result + (name != null ? name.hashCode() : 0);
result = prime * result + (isOpen ? 0 : 1);
return result;
}
Additional Information
I believe this is the best explanation of overriding hashCode().
This answer was posted by dmeister for the following post: SO: Hash Code implementation.
I reference this all the time, and it looks like this functionallity is used in Eclipse when generating the hashCode() method for a given class.
A for nearly all cases reasonable good implementation was proposed in
Josh Bloch's "Effective Java" in item 8. The best thing is to look it
up there because the author explains there why the approach is good.
A short version:
Create a int result and assign a non-zero value.
For every field tested in the equals-Method, calculate a hash code c by:
If the field f is a boolean: calculate (f ? 0 : 1);
If the field f is a byte, char, short or int: calculate (int)f;
If the field f is a long: calculate (int)(f ^ (f >>> 32));
If the field f is a float: calculate Float.floatToIntBits(f);
If the field f is a double: calculate Double.doubleToLongBits(f) and handle the return value like every long value;
If the field f is an object: Use the result of the hashCode() method or 0 if f == null;
If the field f is an array: See every field as separate element and
calculate the hash value in a recursive fashion and combine the values
as described next.
Combine the hash value c with result with:
result = 37 * result + c
Return result
This should result in a proper distribution of hash values for most use situations.
Imagine twins (in real life), even if they have same look and same age and same name, are they equal? No they are not, they are two different "instances".
It is same in Java. Two different instances cannot be (implicitly) equal, because they each exist independently in their part of memory.
However if you want to compare them like that, you can ovveride equals() method or you can create your own new method for comparing.
Even though I set the same id and name to school1 & school2 instances, but school1.equals(school2) returns false, why?
You need to ovveride the equals() method in your School class. Otherwise the default method implementation from Object class.
see default implementation
public boolean More ...equals(Object obj) {
return (this == obj);
}
In your case it is false, since you are creating two objects. making sense ??
For solution Prefer to read.
You have to override the equals() method meaningfully. The default equals() method inherited from Object class check if two reference are referring same object in the memory.
The equals method for class Object implements the most discriminating
possible equivalence relation on objects; that is, for any non-null
reference values x and y, this method returns true if and only if x
and y refer to the same object (x == y has the value true).
You have created 2 new objects. Now you are comparing 2 object references ... You are not comparing field member values.. So comparison is false.
For primitive datatypes, you wouldn't have this problem.
If you do not override the public boolean equals(Object) method, the version in Object.class will be called:
public boolean equals(Object obj) {
return (this == obj);
}
Just compare the references(If they are exactly the same object)!
So, you have to implement your own equals(Object) in the School.class. Compare these fields:
private String name; // use String.equals(String)
private int id; // use ==
private boolean isOpen; // use ==
By default, .equals() does "==" ie., comapring references. You have to override equals().
The simple answer is, equals of the implicit super class Object is being used for comparison.
From the documentation:
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).
Equals method
You would have to override the equals and hashcode method in your Student class.
If you don't override the equals(), then the default equals() in Java.lang.Object will be called:
public boolean equals(Object obj) {
return (this == obj);
}
As you see, it compares the references of two object, so in your case it returns false.
If you want to compare the contents of two object, you can:
#Override
public boolean equals(Object obj) {
// A simple impl. Pls add some checking for null/class type/.. yourself
return this.name.equals(obj.getName()) && this.id == obj.getId() && this.isOpen == isOpen();
}
I have an ArrayList of a class that holds information, and I want to add to objects to this list. I want to check to see if that list already contains a number before adding it to a list.
Normally if it were just a list of strings I would just do something like this
if(!list.contains("this string")){
list.add("this string");
}
but since this is a class, it has more than one variable per index.
An example of the class world be this:
private class From{
private long threadID;
private long date;
private String address;
private String body;
private int type;
private long id;
#Override
public boolean equals(Object obj){
if(obj != null){
if(getClass() != obj.getClass()){
return false;
}else{
final From from = (From)obj;
if((this.address != null) ? (from.address != null) : !this.address.equals(from.address)){
return false;
}
}
}else{
return false;
}
return true;
}
}
I want to see if there is already an entry with the same number, so am I going to have to manually loop through each index and check, or is there an easier way of doing what I want to do?
EDIT:
how i call it
HashSet<From> addresses = new HashSet<From>();
From f = new From();
f.setAddress(messages.getString(3));
f.setBody(messages.getString(0));
f.setDate(messages.getLong(2));
f.setThreadId(messages.getLong(1));
f.setType(1);
if(!addresses.contains(f.address)){
addresses.add(f);
}
Use a Set instead of a List. Sets dont allow duplicates
http://docs.oracle.com/javase/6/docs/api/java/util/HashSet.html
You will also need to override equals in your class so the Set knows if two objects are equal
Example of overriding equals can be found here: How to override equals method in java
You have to override equals(Object o) for this. This is the place where you need to define the logic that would define the equality between two objects.
It is good practice to override hashCode() as well. Read more in the Javadocs for Object.
Another way would be to override equals() for Info object such that two Info objects are equal if they both have the same number. before adding the element into the list just do the equals() test .
You can still use the contains method; A List uses the equals method to determine if the item exists in the list. But you must override equals in your Info object.