Java Set collection - override equals method

前端 未结 5 726
孤城傲影
孤城傲影 2020-12-14 17:08

Is there any way to override the the equals method used by a Set datatype? I wrote a custom equals method for a class called Fee

5条回答
  •  -上瘾入骨i
    2020-12-14 17:37

    Set uses the equals method of the object added to the set. The JavaDoc states

    A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.

    The Set.equals() method is only used to compare two sets for equality. It's never used as part of adding/remove items from the set.

提交回复
热议问题