Could anyone tell me the logic behind this piece of code?
public int compareTo(Holder o) {
if(o == null) return -1;
return this.value.compareTo(o.value);
}
Could anyone tell me the logic behind this piece of code?
public int compareTo(Holder o) {
if(o == null) return -1;
return this.value.compareTo(o.value);
}
It compares this
against another object o
.
If o
is null, then this
is considered smaller than o
(indicated by return value -1
).
Otherwise the fields value
of this
and o
are compared and the result is returned as comparison result (-1
= smaller, 1
= greater, 0
= equal).
The rationale is to have a proper ordering of elements, e.g. to sort a list.