Understanding TreeSet when compareto returns 0

后端 未结 5 804
逝去的感伤
逝去的感伤 2021-01-06 02:30

I have created a Student class like this:

public class Student implements Comparable {

    private String firstName;
    private String lastN         


        
5条回答
  •  日久生厌
    2021-01-06 03:12

    Well, your treeset key values are "A1","B1","A1","A2". Even though you are not overriding equals and hashcode still the default hashcode for "A1" will be same and hence the treeset will consider this key as duplicate key so you will not be able to enter "A1","B2"

提交回复
热议问题