SortedMap.transformValues?

Paul Sandoz paul.sandoz at oracle.com
Thu Jan 17 01:57:37 PST 2013


On Jan 16, 2013, at 6:57 PM, Michael Nascimento <misterm at gmail.com> wrote:

> Hi guys,
> 
> What is the lambda way of achieving:
> 
> http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Maps.html#transformValues(java.util.SortedMap,
> com.google.common.base.Function)
> 
> ?

There is no exact equivalence because the Guava Maps.transformValues creates a view over the original map, it is lazy. 

Lazy map views in the JDK would i think require some closer integration with the existing map implementations rather than using the lambda Stream APIs.

FWIW using the Collectors a copy would have to be made:

SortedMap map = ...
SortedMap copy = map.entrySet().collect(
  groupBy(
    e -> e.getKey(),
    TreeMap::new,
    e -> e.getValue(),
    Collectors.THROWING_MERGER)
);

Quite a mouthful!

If/when we re-introduce MapStream we could do:

  map.stream().mapValue(...).collect(toSortedMap());

But still that is not lazy.

Paul.


More information about the lambda-dev mailing list