I have ArrayList and I would like to check if ArrayList contains specific JSON object. I’ve tried something like
if (list.contains(jsonObject)) {
// Do something
}
else {
// DO somethings else
System.out.println("JSON object added");
}
But, if I created two identical JSON objects the result was always that the condition is false. Is it possible to do it this way? If not what is the best solution to solve my problem?
The result is false because it is another Java object (that means it has another hash code). If you like to compare types in a list, you have to implement hashCode() and equals(). Eclipse provides a function for this:
Source -> generate hashCode() and equals().