Concatenated stream of strings and integers
I am learning Stream and during one such practice session I tried to join 2 streams of different type String and Integer using the code below:
Stream<String> fruitStream = Stream.of("Apple", "Mango", "Muskmalon", "Guvava");
Stream<Integer> vegetablesStream = Stream.of(1, 2, 3, 4);
Stream<String> mixStream = Stream.concat(fruitStream, vegetablesStream.map(i -> i.toString()));
mixStream.forEach(System.out :: println);
It worked correctly and gave me the desired result.
Then I tried using a method reference:
Stream<String> mixStream = Stream.concat(fruitStream, vegetablesStream.map(Integer :: toString));
And he started throwing an error:
Wrong 1st argument type. Found: 'java.util.stream.Stream<java.lang.String>', required: 'java.util.stream.Stream<? extends T>'
concat (java.util.stream.Stream<? extends T>, java.util.stream.Stream<? extends T>) in Stream cannot be applied to (java.util.stream.Stream<java.lang.String>, java.util.stream.Stream<R>)
reason: No compile-time declaration for the method reference is found
When I replaced the method reference with String :: ValueOf it works fine:
Stream<String> mixStream = Stream.concat(fruitStream, vegetablesStream.map(String :: valueOf));
I can't figure out why Integer :: toString failed, but String :: valueOf got through?
source to share
The error message should be Ambiguous method reference: both toString() and toString(int) from the type Integer are eligible
. This is because there can be used toString
, and so toString(int)
.
It could have been both:
.map(i -> i.toString(i))
.map(i -> i.toString())
And it works great because it String::valueOf
doesn't have such an overloaded method, and thus the compiler does a great job of it.
source to share