Today I was asked this interview question:
If I have a
Person
class withname
,age
andsalary
fields, and I put 100 new instances of thisPerson
in anArrayList
, and then doCollections.sort(list)
, then on what parameter will the list be sorted?
I understand that I need to have the Person
class implement Comparable
and then override compareTo
, but if I don't do that, what will happen?
It wouldn't compile: the 1-argument version of Collections.sort
expects a list of Comparable
s. Specifically, a List<T>
where T
implements Comparable<? super T>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With