I am trying to break the rule of set by allowing the same object to be twice (just for fun)
This is my code:
class Person implements Comparable<Person> {
public String firstName;
public Person(String firstName) {
this.firstName = firstName;
}
@Override
public int compareTo(Person o) {
return 4;
}
}
class customTreeSet<T extends Person> extends TreeSet<T> {
private static final long serialVersionUID = 1L;
}
and I call it like this:
Set<Person> set = new customTreeSet<Person>();
set.add(new Person("Forza"));
set.add(new Person("Forza"));
for (Person person : set) {
System.out.println(person.firstName);
}
what I am facing is that when i leave the return as 4
, my set contains two objects, but when i change it to zero
my set contains one object.
i need to know the rule for the returning type specially that it is integer not boolean, if it was boolean, i will say that true means two objects are the same, false means not, but here it is an integer value not boolean.
thanks for your help