vigna / fastutil

fastutil extends the Java™ Collections Framework by providing type-specific maps, sets, lists and queues.
Apache License 2.0
1.81k stars 199 forks source link

Primitive Comparator.comparing(keyExtractor) #312

Open barakugav opened 10 months ago

barakugav commented 10 months ago

The standard java.util.Comparator provide few static methods to create a Comparator from an key extractor function:

static <T, U extends Comparable<U>> Comparator<T> comparing(Function<T, U> keyExtractor);
static <T, U extends Comparable<U>> Comparator<T> comparing(Function<T, U> keyExtractor, Comparator<U> keyComparator);
static <T> Comparator<T> comparingInt(ToIntFunction<T> keyExtractor);
static <T> Comparator<T> comparingLong(ToLongFunction<T> keyExtractor);
static <T> Comparator<T> comparingDouble(ToDoubleFunction<T> keyExtractor);

I find these functions very useful.

Will this be a nice additions to the primitive comparators in fastutil as well? For example in IntComparator. If so, I will be willing to add it :)

vigna commented 10 months ago

Ok but you need to understand the macro system and write a generic implementation...