检查ArrayList是否包含特定的JSON对象

时间:2023-01-15 16:47:39

I have ArrayList and I would like to check if ArrayList contains specific JSON object. I've tried something like

我有ArrayList,我想检查ArrayList是否包含特定的JSON对象。我尝试过类似的东西

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?

但是,如果我创建了两个相同的JSON对象,结果始终是条件为false。有可能这样做吗?如果不是解决我问题的最佳解决方案是什么?

1 个解决方案

#1


0  

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().

结果是错误的,因为它是另一个Java对象(这意味着它有另一个哈希代码)。如果您想比较列表中的类型,则必须实现hashCode()和equals()。 Eclipse为此提供了一个函数:Source - >生成hashCode()和equals()。

#1


0  

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().

结果是错误的,因为它是另一个Java对象(这意味着它有另一个哈希代码)。如果您想比较列表中的类型,则必须实现hashCode()和equals()。 Eclipse为此提供了一个函数:Source - >生成hashCode()和equals()。