I have the sample code below attempting to construct an immutable map out of an iterable:
ImmutableList<Entry<Integer, Double>> list = ImmutableList.of(
new AbstractMap.SimpleEntry<Integer, Double>(4, 2d),
new AbstractMap.SimpleEntry<Integer, Double>(16, 4d));
ImmutableMap.<Integer, Double>builder().putAll(list); // No error
ImmutableMap.<Integer, Double>builder().putAll(() -> list.stream().iterator()); // Error - cannot convert type...
I am getting the error Bad return type in lambda expression: Iterator<Entry<Integer, Double>> cannot be converted to Iterator<Entry<? extends Integer, ? extends Double>>. From my understanding this should be valid because Integer is a valid upper bound of Integer, and Double is a valid upper bound of Double. What is the problem here and how can I fix specifically the iterator code?
I need to fix the iterator code in particular since I am trying to transform another collection into a map like below. I am using guava on android so do not have access to the default guava collect() methods (though I think I can implement them manually if there is really no other way to do this).
ImmutableList<Integer> numberList = ImmutableList.of(2,4,8,12,16,24,32);
ImmutableMap.<Integer, Double>builder().putAll(() ->
numberList.stream().map(num ->
(Entry<Integer, Double>) new AbstractMap.SimpleEntry<Integer, Double>(num, Math.sqrt(num))).iterator());