Replace this lambda with a method reference [duplicate]
up vote
7
down vote
favorite
This question already has an answer here:
How to fix ambiguous type on method reference (toString of an Integer)?
3 answers
I have the following code. Sonar is complaining replace this lambda with a method reference.
Stream.iterate(0, i -> i + 1).limit(100).map(i -> Integer.toString(i));
If I replace it with it code below, it does not compile with compilation error: Type mismatch: cannot convert from Stream<Object>
to <unknown>
.
Stream.iterate(0, i -> i + 1).limit(100).map(Integer::toString);
How is Integer::toString
converting Stream<Object>
to <unknown>
?
java java-8 sonarqube java-stream
marked as duplicate by Holger
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 mins ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
7
down vote
favorite
This question already has an answer here:
How to fix ambiguous type on method reference (toString of an Integer)?
3 answers
I have the following code. Sonar is complaining replace this lambda with a method reference.
Stream.iterate(0, i -> i + 1).limit(100).map(i -> Integer.toString(i));
If I replace it with it code below, it does not compile with compilation error: Type mismatch: cannot convert from Stream<Object>
to <unknown>
.
Stream.iterate(0, i -> i + 1).limit(100).map(Integer::toString);
How is Integer::toString
converting Stream<Object>
to <unknown>
?
java java-8 sonarqube java-stream
marked as duplicate by Holger
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 mins ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
This question already has an answer here:
How to fix ambiguous type on method reference (toString of an Integer)?
3 answers
I have the following code. Sonar is complaining replace this lambda with a method reference.
Stream.iterate(0, i -> i + 1).limit(100).map(i -> Integer.toString(i));
If I replace it with it code below, it does not compile with compilation error: Type mismatch: cannot convert from Stream<Object>
to <unknown>
.
Stream.iterate(0, i -> i + 1).limit(100).map(Integer::toString);
How is Integer::toString
converting Stream<Object>
to <unknown>
?
java java-8 sonarqube java-stream
This question already has an answer here:
How to fix ambiguous type on method reference (toString of an Integer)?
3 answers
I have the following code. Sonar is complaining replace this lambda with a method reference.
Stream.iterate(0, i -> i + 1).limit(100).map(i -> Integer.toString(i));
If I replace it with it code below, it does not compile with compilation error: Type mismatch: cannot convert from Stream<Object>
to <unknown>
.
Stream.iterate(0, i -> i + 1).limit(100).map(Integer::toString);
How is Integer::toString
converting Stream<Object>
to <unknown>
?
This question already has an answer here:
How to fix ambiguous type on method reference (toString of an Integer)?
3 answers
java java-8 sonarqube java-stream
java java-8 sonarqube java-stream
edited 6 hours ago
asked 8 hours ago
fastcodejava
23.6k19109160
23.6k19109160
marked as duplicate by Holger
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 mins ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Holger
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
2 mins ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
8
down vote
It's ambiguous because the static and non-static toString()
methods are both compatible with the functional signature Integer -> String
. You can use String::valueOf
instead.
add a comment |
up vote
5
down vote
You can't put Integer::toString
because Integer
has two implementations that fit to functional interface Function<Integer, String>
, but you can use String::valueOf
instead:
Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList())
1
You can't putInteger::toString
because it acceptsint
and your case you haveInteger
use. That's not correct. Lambdas can implicitly box and unbox.
– shmosel
8 hours ago
@shmosel then why does itIntStream.range(1, 100).mapToObj(Integer::toString).collect(Collectors.toList())
work?
– vlad324
8 hours ago
Because theint
overload is more appropriate for a primitive stream.
– shmosel
8 hours ago
@shmosel yes, you are right about Lambdas can implicitly box and unbox
– vlad324
7 hours ago
add a comment |
up vote
2
down vote
As mentioned by @shmosel already replacing the lambda with a method reference will lead to ambiguity as there's two toString
method of the signarure:
String toString()
static String toString(int i)
because the call to Stream.iterate(0, i -> i + 1)
returns a Stream<Integer>
when you call map
with the method reference Integer::toString
the compiler is not sure whether you meant to do Integer.toString(i)
or i.toString()
hence the compilation error.
So here are other options to what's already been provided:
instead of Stream.iterate
you can use IntStream.iterate
then call mapToObj
:
IntStream.iterate(0, i -> i + 1) // IntStream
.limit(100) // IntStream
.mapToObj(Integer::toString); // i1 -> Integer.toString(i1)
Another thing suggested by intelliJ
is that you can actually do:
Stream.iterate(0, i -> i + 1) // Stream<Integer>
.limit(100) // Stream<Integer>
.map(Object::toString); // integer -> integer.toString()
where Object::toString
is equivalent to the lambda integer -> integer.toString()
on another note, it's interesting that Sonar is suggesting to replace the lambda with a method reference in the code you've shown. intelliJ IDEA was smart enough not to suggest it.
add a comment |
up vote
0
down vote
I think that IntStream
is better for your code:
List<String> numbers = IntStream.range(0, 100)
.mapToObj(String::valueOf)
.collect(Collectors.toList());
Or for your example do use String.valueOf
to conver int -> String
:
List<String> numbers = Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList());
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
It's ambiguous because the static and non-static toString()
methods are both compatible with the functional signature Integer -> String
. You can use String::valueOf
instead.
add a comment |
up vote
8
down vote
It's ambiguous because the static and non-static toString()
methods are both compatible with the functional signature Integer -> String
. You can use String::valueOf
instead.
add a comment |
up vote
8
down vote
up vote
8
down vote
It's ambiguous because the static and non-static toString()
methods are both compatible with the functional signature Integer -> String
. You can use String::valueOf
instead.
It's ambiguous because the static and non-static toString()
methods are both compatible with the functional signature Integer -> String
. You can use String::valueOf
instead.
answered 8 hours ago
shmosel
35.6k43892
35.6k43892
add a comment |
add a comment |
up vote
5
down vote
You can't put Integer::toString
because Integer
has two implementations that fit to functional interface Function<Integer, String>
, but you can use String::valueOf
instead:
Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList())
1
You can't putInteger::toString
because it acceptsint
and your case you haveInteger
use. That's not correct. Lambdas can implicitly box and unbox.
– shmosel
8 hours ago
@shmosel then why does itIntStream.range(1, 100).mapToObj(Integer::toString).collect(Collectors.toList())
work?
– vlad324
8 hours ago
Because theint
overload is more appropriate for a primitive stream.
– shmosel
8 hours ago
@shmosel yes, you are right about Lambdas can implicitly box and unbox
– vlad324
7 hours ago
add a comment |
up vote
5
down vote
You can't put Integer::toString
because Integer
has two implementations that fit to functional interface Function<Integer, String>
, but you can use String::valueOf
instead:
Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList())
1
You can't putInteger::toString
because it acceptsint
and your case you haveInteger
use. That's not correct. Lambdas can implicitly box and unbox.
– shmosel
8 hours ago
@shmosel then why does itIntStream.range(1, 100).mapToObj(Integer::toString).collect(Collectors.toList())
work?
– vlad324
8 hours ago
Because theint
overload is more appropriate for a primitive stream.
– shmosel
8 hours ago
@shmosel yes, you are right about Lambdas can implicitly box and unbox
– vlad324
7 hours ago
add a comment |
up vote
5
down vote
up vote
5
down vote
You can't put Integer::toString
because Integer
has two implementations that fit to functional interface Function<Integer, String>
, but you can use String::valueOf
instead:
Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList())
You can't put Integer::toString
because Integer
has two implementations that fit to functional interface Function<Integer, String>
, but you can use String::valueOf
instead:
Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList())
edited 7 hours ago
answered 8 hours ago
vlad324
53228
53228
1
You can't putInteger::toString
because it acceptsint
and your case you haveInteger
use. That's not correct. Lambdas can implicitly box and unbox.
– shmosel
8 hours ago
@shmosel then why does itIntStream.range(1, 100).mapToObj(Integer::toString).collect(Collectors.toList())
work?
– vlad324
8 hours ago
Because theint
overload is more appropriate for a primitive stream.
– shmosel
8 hours ago
@shmosel yes, you are right about Lambdas can implicitly box and unbox
– vlad324
7 hours ago
add a comment |
1
You can't putInteger::toString
because it acceptsint
and your case you haveInteger
use. That's not correct. Lambdas can implicitly box and unbox.
– shmosel
8 hours ago
@shmosel then why does itIntStream.range(1, 100).mapToObj(Integer::toString).collect(Collectors.toList())
work?
– vlad324
8 hours ago
Because theint
overload is more appropriate for a primitive stream.
– shmosel
8 hours ago
@shmosel yes, you are right about Lambdas can implicitly box and unbox
– vlad324
7 hours ago
1
1
You can't put
Integer::toString
because it accepts int
and your case you have Integer
use. That's not correct. Lambdas can implicitly box and unbox.– shmosel
8 hours ago
You can't put
Integer::toString
because it accepts int
and your case you have Integer
use. That's not correct. Lambdas can implicitly box and unbox.– shmosel
8 hours ago
@shmosel then why does it
IntStream.range(1, 100).mapToObj(Integer::toString).collect(Collectors.toList())
work?– vlad324
8 hours ago
@shmosel then why does it
IntStream.range(1, 100).mapToObj(Integer::toString).collect(Collectors.toList())
work?– vlad324
8 hours ago
Because the
int
overload is more appropriate for a primitive stream.– shmosel
8 hours ago
Because the
int
overload is more appropriate for a primitive stream.– shmosel
8 hours ago
@shmosel yes, you are right about Lambdas can implicitly box and unbox
– vlad324
7 hours ago
@shmosel yes, you are right about Lambdas can implicitly box and unbox
– vlad324
7 hours ago
add a comment |
up vote
2
down vote
As mentioned by @shmosel already replacing the lambda with a method reference will lead to ambiguity as there's two toString
method of the signarure:
String toString()
static String toString(int i)
because the call to Stream.iterate(0, i -> i + 1)
returns a Stream<Integer>
when you call map
with the method reference Integer::toString
the compiler is not sure whether you meant to do Integer.toString(i)
or i.toString()
hence the compilation error.
So here are other options to what's already been provided:
instead of Stream.iterate
you can use IntStream.iterate
then call mapToObj
:
IntStream.iterate(0, i -> i + 1) // IntStream
.limit(100) // IntStream
.mapToObj(Integer::toString); // i1 -> Integer.toString(i1)
Another thing suggested by intelliJ
is that you can actually do:
Stream.iterate(0, i -> i + 1) // Stream<Integer>
.limit(100) // Stream<Integer>
.map(Object::toString); // integer -> integer.toString()
where Object::toString
is equivalent to the lambda integer -> integer.toString()
on another note, it's interesting that Sonar is suggesting to replace the lambda with a method reference in the code you've shown. intelliJ IDEA was smart enough not to suggest it.
add a comment |
up vote
2
down vote
As mentioned by @shmosel already replacing the lambda with a method reference will lead to ambiguity as there's two toString
method of the signarure:
String toString()
static String toString(int i)
because the call to Stream.iterate(0, i -> i + 1)
returns a Stream<Integer>
when you call map
with the method reference Integer::toString
the compiler is not sure whether you meant to do Integer.toString(i)
or i.toString()
hence the compilation error.
So here are other options to what's already been provided:
instead of Stream.iterate
you can use IntStream.iterate
then call mapToObj
:
IntStream.iterate(0, i -> i + 1) // IntStream
.limit(100) // IntStream
.mapToObj(Integer::toString); // i1 -> Integer.toString(i1)
Another thing suggested by intelliJ
is that you can actually do:
Stream.iterate(0, i -> i + 1) // Stream<Integer>
.limit(100) // Stream<Integer>
.map(Object::toString); // integer -> integer.toString()
where Object::toString
is equivalent to the lambda integer -> integer.toString()
on another note, it's interesting that Sonar is suggesting to replace the lambda with a method reference in the code you've shown. intelliJ IDEA was smart enough not to suggest it.
add a comment |
up vote
2
down vote
up vote
2
down vote
As mentioned by @shmosel already replacing the lambda with a method reference will lead to ambiguity as there's two toString
method of the signarure:
String toString()
static String toString(int i)
because the call to Stream.iterate(0, i -> i + 1)
returns a Stream<Integer>
when you call map
with the method reference Integer::toString
the compiler is not sure whether you meant to do Integer.toString(i)
or i.toString()
hence the compilation error.
So here are other options to what's already been provided:
instead of Stream.iterate
you can use IntStream.iterate
then call mapToObj
:
IntStream.iterate(0, i -> i + 1) // IntStream
.limit(100) // IntStream
.mapToObj(Integer::toString); // i1 -> Integer.toString(i1)
Another thing suggested by intelliJ
is that you can actually do:
Stream.iterate(0, i -> i + 1) // Stream<Integer>
.limit(100) // Stream<Integer>
.map(Object::toString); // integer -> integer.toString()
where Object::toString
is equivalent to the lambda integer -> integer.toString()
on another note, it's interesting that Sonar is suggesting to replace the lambda with a method reference in the code you've shown. intelliJ IDEA was smart enough not to suggest it.
As mentioned by @shmosel already replacing the lambda with a method reference will lead to ambiguity as there's two toString
method of the signarure:
String toString()
static String toString(int i)
because the call to Stream.iterate(0, i -> i + 1)
returns a Stream<Integer>
when you call map
with the method reference Integer::toString
the compiler is not sure whether you meant to do Integer.toString(i)
or i.toString()
hence the compilation error.
So here are other options to what's already been provided:
instead of Stream.iterate
you can use IntStream.iterate
then call mapToObj
:
IntStream.iterate(0, i -> i + 1) // IntStream
.limit(100) // IntStream
.mapToObj(Integer::toString); // i1 -> Integer.toString(i1)
Another thing suggested by intelliJ
is that you can actually do:
Stream.iterate(0, i -> i + 1) // Stream<Integer>
.limit(100) // Stream<Integer>
.map(Object::toString); // integer -> integer.toString()
where Object::toString
is equivalent to the lambda integer -> integer.toString()
on another note, it's interesting that Sonar is suggesting to replace the lambda with a method reference in the code you've shown. intelliJ IDEA was smart enough not to suggest it.
edited 15 mins ago
answered 1 hour ago
Aomine
35.3k62859
35.3k62859
add a comment |
add a comment |
up vote
0
down vote
I think that IntStream
is better for your code:
List<String> numbers = IntStream.range(0, 100)
.mapToObj(String::valueOf)
.collect(Collectors.toList());
Or for your example do use String.valueOf
to conver int -> String
:
List<String> numbers = Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList());
add a comment |
up vote
0
down vote
I think that IntStream
is better for your code:
List<String> numbers = IntStream.range(0, 100)
.mapToObj(String::valueOf)
.collect(Collectors.toList());
Or for your example do use String.valueOf
to conver int -> String
:
List<String> numbers = Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList());
add a comment |
up vote
0
down vote
up vote
0
down vote
I think that IntStream
is better for your code:
List<String> numbers = IntStream.range(0, 100)
.mapToObj(String::valueOf)
.collect(Collectors.toList());
Or for your example do use String.valueOf
to conver int -> String
:
List<String> numbers = Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList());
I think that IntStream
is better for your code:
List<String> numbers = IntStream.range(0, 100)
.mapToObj(String::valueOf)
.collect(Collectors.toList());
Or for your example do use String.valueOf
to conver int -> String
:
List<String> numbers = Stream.iterate(0, i -> i + 1)
.limit(100)
.map(String::valueOf)
.collect(Collectors.toList());
edited 57 mins ago
answered 1 hour ago
oleg.cherednik
4,9092916
4,9092916
add a comment |
add a comment |