What happens if we override only hashCode() in a class and use it in a Set?

前端 未结 5 751
既然无缘
既然无缘 2020-12-15 11:25

This may not be the real world scenario but just curious to know what happens, below is the code.

I am creating a set of object of class UsingSet. Accor

5条回答
  •  臣服心动
    2020-12-15 11:47

    Set will behave differently.

    Uniqueness wont happen. Because unique will be achieved by both hashcode and equals methods.
    output will be liked this s = [A, a, b, 1] instead of early one.

    Apart that remove and contains all wont work.

提交回复
热议问题