Why Guava does not provide a way to transform map keys

前端 未结 1 1970
日久生厌
日久生厌 2021-02-19 20:06

This question is kind of already posted here: How to convert Map to Map using guava

I think the answer of CollinD is appropriat

相关标签:
1条回答
  • 2021-02-19 20:35

    As @CollinD suggests, there's no way to do this in a lazy way. To implement get, you have to convert all the keys with your transformation function (to ensure any duplicates are discovered).

    So applying Function<K,NewK> to Map<K,V> is out.

    You could safely apply Function<NewK,K> to the map:

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

    I don't see a Guava shorthand for that--it may just not be useful enough. You could get similar results with:

    Function<NewK,V> newFn = Functions.compose(Functions.forMap(map), fn);
    
    0 讨论(0)
提交回复
热议问题