When and why does List remove(Object object) return false

后端 未结 3 1953
情歌与酒
情歌与酒 2020-12-19 21:27

When and why does the method boolean java.util.List.remove(Object object) return false?

The documentation states

[The method ret

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-19 22:03

    If the Object passed in is not actually in the list, it wouldn't take any effect on the list and therefore return false.

    Edit (thanks Jai): The method uses the specific Objects equals() method to determine if it contains that object. So, if you have custom objects make sure you override the equals() method, and also the hashCode() (to maintain the general contract between the methods).

提交回复
热议问题