I'm new to the Guava library and trying to use some of its classes to simplify my code. I've run across the need to sort a Map by value. A quick search found this post which posts an accepted answer as the following code snippet:
Ordering<Map.Entry<Key, Value>> entryOrdering = Ordering.from(valueComparator)
.onResultOf(new Function<Entry<Key, Value>, Value>() {
public Value apply(Entry<Key, Value> entry) {
return entry.getValue();
}
}).reverse();
// Desired entries in desired order. Put them in an ImmutableMap in this order.
ImmutableMap.Builder<Key, Value> builder = ImmutableMap.builder();
for (Entry<Key, Value> entry :
entryOrdering.sortedCopy(map.entrySet())) {
builder.put(entry.getKey(), entry.getValue());
}
return builder.build();
// ImmutableMap iterates over the entries in the desired order
Can someone please clarify for me how this works? I am failing to understand something.
The definition of Ordering is Ordering<T>. In this case <T> would be Map.Entry. Furthermore, the method signature of onResultOf is onResultOf(Function<F,? extends T> function).
In the above code, onResultOf is being called with the following parameter:
onResultOf(new Function<Entry<Key, Value>, Value>())
Which in this case would imply that:
<F> = Entry<Key, Value>
<? extends T> = Value
Which in turn implies that Value is a type that would extend a Map.Entry.
How is that possible? How can Value be a type that would extend Map.Entry, when the entry map contains Key, Value?
I am sure I am misreading or misunderstanding something, but if anyone can shed some light on this, I am hoping it will help me understand Guava and the Ordering() class a little better. Especially how the onResultOf(Function) works.