Building the following code with Java 10.0.2+13, Gradle 4.9 & Guava 26.0-jre outputs this error:
Error:
argument mismatch; bad return type in method reference
Iterator<Entry<String,ImmutableMap<Path,String>>>
cannot be converted to
Iterator<Entry<? extends String,? extends ImmutableMap<Path,String>>>
Code:
final Map<String, Map<Path, String>> m = new HashMap<>();
// put entries into m
final ImmutableMap<String, ImmutableMap<Path, String>> im =
ImmutableMap.<String, ImmutableMap<Path, String>>copyOf(
m.entrySet().stream().map(
e -> immutableEntry(
e.getKey(),
ImmutableMap.copyOf(e.getValue())
)
)::iterator
)
;
Why do String
& ImmutableMap<Path,String>
not satisfy ? extends String
& ? extends ImmutableMap<Path,String>
, respectively?
How can I get this to properly compile?
Maybe this does not directly answer your question, but is using a Collector
not an option?
Like this:
ImmutableMap<String, ImmutableMap<Path, String>> im = m.entrySet().stream()
.collect(ImmutableMap.toImmutableMap(
Map.Entry::getKey, e -> ImmutableMap.copyOf(e.getValue())
));
EDIT: Just as a curosity, this gets your code to compile (but please use the Collector
-version instead, it's much more clear than the code below):
ImmutableMap<String, ImmutableMap<Path, String>> im = ImmutableMap.copyOf(
m.entrySet().stream()
.<Map.Entry<? extends String, ? extends ImmutableMap<Path, String>>>map(e -> immutableEntry(e.getKey(), ImmutableMap.copyOf(e.getValue())))
::iterator
);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments