I am trying to compare two objects to see if they are the same. I create the first List from JSON code and populate the List (data) with an instance of InstructionModel. I then compare the data to a database by selecting the data and creating a new instance (model) of InstructionModel. But the result is always false, what am I doing wrong in my code?
exists = data.contains(model);
if(!exists)
{
//Do Some Stuff Here
}
Data from a List<InstructionModel>:

Data in InstructionModel Object:

You need to override the equals-method for your InstructionModel class.
In this method you compare your values and return true if they match, false if they don’t.
The
List#containsfunction will use theObject#equalsfunction of the objects in the container. If this function is not overriden it will compare references only, not the actual values.Addition:
As the comments mention, if you override the ‘equals’ method, than you need to override the ‘hashCode’ method, too (and vice versa). Collections will work with these methods to compare your instances of objects. For example: If you don’t override both, two instances might be considered the same in a List, but not the same in a ‘Hashmap’.