2022-08-23

How do I map a Comparator

I have a comparator of type Comparator<Integer> and a function Function<Pair<Integer,?>,Integer> expressed as Pair::left (that returns an Integer).

I need to obtain a comparator of type Comparator<Pair<Integer,?>>.

If I wanted to simply map a function Function<T,U> to a resulting function Function<T,V> though a function Function<U,V> I could simply apply andThen() method like this:

Function<Integer, String> toBinary = Integer::toBinaryString;
Function<Pair<Integer, ?>, Integer> left = Pair::left;

var pairToBinary = left.andThen(toBinary); // has type Function<Pair<Integer, ?>, String>

Is it possible to obtain Comparator<Pair<Integer,?>> in a similar way?



No comments:

Post a Comment