Java – why doesn’t guava provide a way to convert map keys

This question has been posted here:

I think collind's answer is appropriate:

This is true, but actually I don't understand why it's not because:

>When the key transformation occurs, if two keys are "merged", a runtime exception may be thrown, or we can pass a flag to instruct guava to obtain any value of multiple possible values for the newly calculated key (failfast / failsafe possibility) > we can have a map that generates a Multimap transformKeys

Can't I see any shortcomings in doing such a thing?

Solution

As @ collind said, there is no way to do this in a lazy way To implement get, you must convert all keys using a conversion function (to ensure that any duplicates are found)

Therefore, the application function < K, newk > maps < K, V >

You can safely apply function < newk, k > to the map:

V value = innerMap.get( fn.apply(newK) );

I don't see the shorthand of guava - it may not be useful enough You can get similar results:

Function<NewK,V> newFn = Functions.compose(Functions.forMap(map),fn);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>