The following code gives me compilation error:
//C1 c1 = ....;
//C2 c2 = ....;
List<Pair<Pair<C1, String>, C2>>> l = someMethod().stream()
.map(item -> ImmutablePair.of(ImmutablePair.of(c1, "hello"), c2))
.collect(Collectors.toList());
But when I change it to the following, it works fine:
//C1 c1 = ....;
//C2 c2 = ....;
List<Pair<Pair<C1, String>, C2>>> l = someMethod().stream()
.map(item -> {
Pair<Pair<C1, String>, C2> r = ImmutablePair.of(ImmutablePair.of(c1, "hello"), c2);
return r;
})
.collect(Collectors.toList());
I thought maybe it's because of casting (which I don't get why it should need to be casted) and I change the code to the following, but I still see the "Incompatible type" error:
//C1 c1 = ....;
//C2 c2 = ....;
List<Pair<Pair<C1, String>, C2>>> l = someMethod().stream()
.map(item ->
(Pair<Pair<C1, String>, C2>)ImmutablePair.of(ImmutablePair.of(c1, "hello"), c2)
})
.collect(Collectors.toList());
How can I write this in one line to work? and why doesn't it figure out itself? What's the ambiguity?