OCA, Java SE 8
Omkar Shetkar wrote:Consider implementing Comparable interface for FacebookUser class. Compare friends list size in compareTo() method.
Zach and I suggest using Comparable instead of compareTo()
Norm Radder wrote:Is that a typo?
Comparators are external to the file and allow a list of objects to have different Comparators for each property in the class that you want to sort on.
There are three kinds of actuaries: those who can count, and those who can't.
kennith stomps wrote:I still have gained no further insight from any of these answers. It is as if no one here is really allowed to answer a question, only beat around the bush
Junilu Lacar wrote:
Omkar Shetkar wrote:Consider implementing Comparable interface for FacebookUser class. Compare friends list size in compareTo() method.
Zach's suggestion to use a Comparator is the better way to go, in my opinion. The semantics of Comparable are narrower and are better suited for comparing things based on more intrinsic attributes. A Comparator has much looser semantics and is better suited for more arbitrary criteria. @OP wants to order the users according to the number of friends they have. This fits better with my idea of what a Comparator would be for.
OCA, Java SE 8
Stephan van Hulst wrote:you can easily create a comparator to pass to the sort() method by calling the factory methods on the Comparator interface, and passing a lambda expression or method handle that describes the property that you want to compare by.
Piet Souris wrote:It is an fine case for a static method in the Comparator interface (Comparator.comparingInt(function that maps a facebookuser to the size of its friends list), eventually followed by .thenComparing(perhaps on name?). Do have a look, it makes for elegant and short code.
OP wrote:I still have gained no further insight from any of these answers. It is as if no one here is really allowed to answer a question, only beat around the bush.
There are three kinds of actuaries: those who can count, and those who can't.
Junilu Lacar wrote:I didn't notice there was an earlier suggestion, so in all fairness, @Piet deserves a cow, too!
There are three kinds of actuaries: those who can count, and those who can't.
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|