Why Guava does not provide a way to transform map keys

前端 未结 1 1959
日久生厌
日久生厌 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 to Map is out.

    You could safely apply Function 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 newFn = Functions.compose(Functions.forMap(map), fn);
    

    0 讨论(0)
提交回复
热议问题