How can I use the 'unique' method in the TotallyLazy library for Java and Objective-C?
I have the following code, but I cannot complete it because I'm not sure how the Callable1 instance passed into 'unique' should be composed. Here's what I have so far:
seq
.sort(new Comparator<T>() {
@Override
public int compare(
final T pt1,
final T pt2
) {
return pt1.compareTo(pt2);
}
})
.unique(new Callable1<T,T>() {
@Override
public T call(final T pt) throws Exception {
final int result = pt.compareTo(..?);
return result != 0;
}});
As you can see, I can sort successfully, but when it comes to "result = ..." in the 'unique' call, what should I put?
Firstly for sorting you should probably use one of the provided Comparators:
seq.sort(Comparators.<T>ascending());
Then you could just unique
with no arguments if uniqueness is based on equality of T as a whole. The overload for unique
allows one to get a unique sequence based on some property of T
.
For example if you had a Sequence<User>
you could say I want them to be unique based on their firstName
field:
class User {
String firstName, lastName;
User(...){...} // Constructor
}
sequence(new User("Chris", "Nash")).unique(user -> user.firstName)
TotallyLazy has extensive Tests that document the features but the first place to start would always be the SequenceTest or have a look in the whole test package for more examples.