1

I'm getting a compilation error on:

@Override
public Map<String, T> loadAll(Iterable<? extends String> keys) {
    return Stream.of(keys)
      .collect(Collectors.toMap(Function.identity(), this::load));
}

The compiler message is:

The method collect(Collector<? super Iterable<capture#1-of ? extends String>,A,R>) in the type Stream<Iterable<capture#1-of ? extends String>> is not applicable for the arguments (Collector<String,capture#2-of ?,Map<Object,Reference>>)

Any work around?

Jordi
  • 20,868
  • 39
  • 149
  • 333

1 Answers1

3

It seems that you think that Stream.of(keys) will create a Stream<String>, but that is not the case. To create a Stream from an Iterable, you should use StreamSupport#stream:

public Map<String, T> loadAll(Iterable<? extends String> keys) {
    return StreamSupport.stream(keys.spliterator(), false)
                        .collect(Collectors.toMap(Function.identity(), this::load));
}

This assumes that your load method returns T.

Jacob G.
  • 28,856
  • 5
  • 62
  • 116