how to check if object already exists in a list

前端 未结 9 1784
梦谈多话
梦谈多话 2020-11-28 05:41

I have a list

  List myList

and I am adding items to a list and I want to check if that object is already in the list.

9条回答
  •  春和景丽
    2020-11-28 05:53

    Another point to mention is that you should ensure that your equality function is as you expect. You should override the equals method to set up what properties of your object have to match for two instances to be considered equal.

    Then you can just do mylist.contains(item)

提交回复
热议问题