IntStream rangeClosed unable to return value other than int [duplicate]
This question already has an answer here:
Why can't I map integers to strings when streaming from an array?
5 answers
Why is this getting error? I thought map
can return any value.
var s = IntStream.rangeClosed(1, 5).map(String::valueOf).collect(Collectors.toList());
| Error: | incompatible types: bad return type in method reference |
java.lang.String cannot be converted to int | var s =
IntStream.rangeClosed(1,
5).map(String::valueOf).collect(Collectors.toList()); |
^-------------^
java lambda java-8 java-stream
marked as duplicate by Mark Rotteveel
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 days 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 |
This question already has an answer here:
Why can't I map integers to strings when streaming from an array?
5 answers
Why is this getting error? I thought map
can return any value.
var s = IntStream.rangeClosed(1, 5).map(String::valueOf).collect(Collectors.toList());
| Error: | incompatible types: bad return type in method reference |
java.lang.String cannot be converted to int | var s =
IntStream.rangeClosed(1,
5).map(String::valueOf).collect(Collectors.toList()); |
^-------------^
java lambda java-8 java-stream
marked as duplicate by Mark Rotteveel
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 days 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.
1
Note that this post is tagged java-8, but thevar
keyword is available from Java 9.
– MC Emperor
2 days ago
2
@MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
– nullpointer
2 days ago
add a comment |
This question already has an answer here:
Why can't I map integers to strings when streaming from an array?
5 answers
Why is this getting error? I thought map
can return any value.
var s = IntStream.rangeClosed(1, 5).map(String::valueOf).collect(Collectors.toList());
| Error: | incompatible types: bad return type in method reference |
java.lang.String cannot be converted to int | var s =
IntStream.rangeClosed(1,
5).map(String::valueOf).collect(Collectors.toList()); |
^-------------^
java lambda java-8 java-stream
This question already has an answer here:
Why can't I map integers to strings when streaming from an array?
5 answers
Why is this getting error? I thought map
can return any value.
var s = IntStream.rangeClosed(1, 5).map(String::valueOf).collect(Collectors.toList());
| Error: | incompatible types: bad return type in method reference |
java.lang.String cannot be converted to int | var s =
IntStream.rangeClosed(1,
5).map(String::valueOf).collect(Collectors.toList()); |
^-------------^
This question already has an answer here:
Why can't I map integers to strings when streaming from an array?
5 answers
java lambda java-8 java-stream
java lambda java-8 java-stream
edited 2 days ago
Nicholas K
5,74451031
5,74451031
asked 2 days ago
Julez Jupiter
326211
326211
marked as duplicate by Mark Rotteveel
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 days 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 Mark Rotteveel
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 days 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.
1
Note that this post is tagged java-8, but thevar
keyword is available from Java 9.
– MC Emperor
2 days ago
2
@MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
– nullpointer
2 days ago
add a comment |
1
Note that this post is tagged java-8, but thevar
keyword is available from Java 9.
– MC Emperor
2 days ago
2
@MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
– nullpointer
2 days ago
1
1
Note that this post is tagged java-8, but the
var
keyword is available from Java 9.– MC Emperor
2 days ago
Note that this post is tagged java-8, but the
var
keyword is available from Java 9.– MC Emperor
2 days ago
2
2
@MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
– nullpointer
2 days ago
@MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
– nullpointer
2 days ago
add a comment |
4 Answers
4
active
oldest
votes
Use mapToObj:
var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
map of IntStream
can only map an int
value to another int
value. It accepts an IntUnaryOperator
(which accepts an int
and returns an int
) as the mapper function, and returns an IntStream
.
On the other hand, mapToObj
allows you to map an int
value to any reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
. It accepts an IntFunction<? extends U>
(which accepts an int
and returns a reference type) as the mapper function, and returns a Stream<U>
.
add a comment |
Use mapToObj instead :
IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
add a comment |
While the aforementioned answers are correct and mapToObj
is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.
it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.
So, let's go through the relevant stream pipeline operations:
IntStream.rangeClosed
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endInclusive (inclusive) by an incremental step of 1.
invoking map
on an IntStream
is expected to return an IntStream
as per the documentation:
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
As well as that it's important to note that the method declaration for map
is as follows:
IntStream map(IntUnaryOperator mapper)
i.e. it takes a IntUnaryOperator
which in fact represents an operation on a single int-valued operand that produces an int-valued result.
However, you're passing a function String::valueOf
which consumes an int
as we're dealing with an IntStream
and returns a String
thus not compliant with IntUnaryOperator
and this is the cause of the problem.
Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R>
as a result then mapToObj
is the way to go.
mapToObj
is declared as:
mapToObj(IntFunction<? extends U> mapper)
IntFunction
represents a function that accepts an int-valued argument and produces a result and this result is of type R
which means you'll have a Stream<R>
after the mapToObj
.
add a comment |
Alternatively, you could use IntStream.boxed
as :
var s = IntStream.rangeClosed(1, 5) // IntStream
.boxed() // Stream<Integer>
.map(String::valueOf) // Stream<String>
.collect(Collectors.toList());
since the IntStream
originally is a sequence of primitive int-values elements.
Another variant of performing such an operation would be :
var s = IntStream.rangeClosed(1, 5)
.boxed()
.map(a -> Integer.toString(a))
.collect(Collectors.toList());
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use mapToObj:
var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
map of IntStream
can only map an int
value to another int
value. It accepts an IntUnaryOperator
(which accepts an int
and returns an int
) as the mapper function, and returns an IntStream
.
On the other hand, mapToObj
allows you to map an int
value to any reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
. It accepts an IntFunction<? extends U>
(which accepts an int
and returns a reference type) as the mapper function, and returns a Stream<U>
.
add a comment |
Use mapToObj:
var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
map of IntStream
can only map an int
value to another int
value. It accepts an IntUnaryOperator
(which accepts an int
and returns an int
) as the mapper function, and returns an IntStream
.
On the other hand, mapToObj
allows you to map an int
value to any reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
. It accepts an IntFunction<? extends U>
(which accepts an int
and returns a reference type) as the mapper function, and returns a Stream<U>
.
add a comment |
Use mapToObj:
var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
map of IntStream
can only map an int
value to another int
value. It accepts an IntUnaryOperator
(which accepts an int
and returns an int
) as the mapper function, and returns an IntStream
.
On the other hand, mapToObj
allows you to map an int
value to any reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
. It accepts an IntFunction<? extends U>
(which accepts an int
and returns a reference type) as the mapper function, and returns a Stream<U>
.
Use mapToObj:
var s = IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
map of IntStream
can only map an int
value to another int
value. It accepts an IntUnaryOperator
(which accepts an int
and returns an int
) as the mapper function, and returns an IntStream
.
On the other hand, mapToObj
allows you to map an int
value to any reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
. It accepts an IntFunction<? extends U>
(which accepts an int
and returns a reference type) as the mapper function, and returns a Stream<U>
.
edited 2 days ago
answered 2 days ago
Eran
279k37452538
279k37452538
add a comment |
add a comment |
Use mapToObj instead :
IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
add a comment |
Use mapToObj instead :
IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
add a comment |
Use mapToObj instead :
IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
Use mapToObj instead :
IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).collect(Collectors.toList());
answered 2 days ago
Nicholas K
5,74451031
5,74451031
add a comment |
add a comment |
While the aforementioned answers are correct and mapToObj
is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.
it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.
So, let's go through the relevant stream pipeline operations:
IntStream.rangeClosed
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endInclusive (inclusive) by an incremental step of 1.
invoking map
on an IntStream
is expected to return an IntStream
as per the documentation:
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
As well as that it's important to note that the method declaration for map
is as follows:
IntStream map(IntUnaryOperator mapper)
i.e. it takes a IntUnaryOperator
which in fact represents an operation on a single int-valued operand that produces an int-valued result.
However, you're passing a function String::valueOf
which consumes an int
as we're dealing with an IntStream
and returns a String
thus not compliant with IntUnaryOperator
and this is the cause of the problem.
Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R>
as a result then mapToObj
is the way to go.
mapToObj
is declared as:
mapToObj(IntFunction<? extends U> mapper)
IntFunction
represents a function that accepts an int-valued argument and produces a result and this result is of type R
which means you'll have a Stream<R>
after the mapToObj
.
add a comment |
While the aforementioned answers are correct and mapToObj
is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.
it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.
So, let's go through the relevant stream pipeline operations:
IntStream.rangeClosed
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endInclusive (inclusive) by an incremental step of 1.
invoking map
on an IntStream
is expected to return an IntStream
as per the documentation:
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
As well as that it's important to note that the method declaration for map
is as follows:
IntStream map(IntUnaryOperator mapper)
i.e. it takes a IntUnaryOperator
which in fact represents an operation on a single int-valued operand that produces an int-valued result.
However, you're passing a function String::valueOf
which consumes an int
as we're dealing with an IntStream
and returns a String
thus not compliant with IntUnaryOperator
and this is the cause of the problem.
Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R>
as a result then mapToObj
is the way to go.
mapToObj
is declared as:
mapToObj(IntFunction<? extends U> mapper)
IntFunction
represents a function that accepts an int-valued argument and produces a result and this result is of type R
which means you'll have a Stream<R>
after the mapToObj
.
add a comment |
While the aforementioned answers are correct and mapToObj
is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.
it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.
So, let's go through the relevant stream pipeline operations:
IntStream.rangeClosed
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endInclusive (inclusive) by an incremental step of 1.
invoking map
on an IntStream
is expected to return an IntStream
as per the documentation:
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
As well as that it's important to note that the method declaration for map
is as follows:
IntStream map(IntUnaryOperator mapper)
i.e. it takes a IntUnaryOperator
which in fact represents an operation on a single int-valued operand that produces an int-valued result.
However, you're passing a function String::valueOf
which consumes an int
as we're dealing with an IntStream
and returns a String
thus not compliant with IntUnaryOperator
and this is the cause of the problem.
Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R>
as a result then mapToObj
is the way to go.
mapToObj
is declared as:
mapToObj(IntFunction<? extends U> mapper)
IntFunction
represents a function that accepts an int-valued argument and produces a result and this result is of type R
which means you'll have a Stream<R>
after the mapToObj
.
While the aforementioned answers are correct and mapToObj
is the idiomatic approach to proceed with, I think it's important to understand why the problem arises and thus in future cases, you'll know how to decipher the problem simply by going through the documentation.
it's a very important skill for a programmer to dig into the documentation when not understanding the workings of a specific method.
So, let's go through the relevant stream pipeline operations:
IntStream.rangeClosed
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endInclusive (inclusive) by an incremental step of 1.
invoking map
on an IntStream
is expected to return an IntStream
as per the documentation:
Returns a stream consisting of the results of applying the given
function to the elements of this stream.
As well as that it's important to note that the method declaration for map
is as follows:
IntStream map(IntUnaryOperator mapper)
i.e. it takes a IntUnaryOperator
which in fact represents an operation on a single int-valued operand that produces an int-valued result.
However, you're passing a function String::valueOf
which consumes an int
as we're dealing with an IntStream
and returns a String
thus not compliant with IntUnaryOperator
and this is the cause of the problem.
Whenever you want to take a primitive stream specialization and perform some mapping function and in turn yield a Stream<R>
as a result then mapToObj
is the way to go.
mapToObj
is declared as:
mapToObj(IntFunction<? extends U> mapper)
IntFunction
represents a function that accepts an int-valued argument and produces a result and this result is of type R
which means you'll have a Stream<R>
after the mapToObj
.
edited 2 days ago
answered 2 days ago
Aomine
40k73770
40k73770
add a comment |
add a comment |
Alternatively, you could use IntStream.boxed
as :
var s = IntStream.rangeClosed(1, 5) // IntStream
.boxed() // Stream<Integer>
.map(String::valueOf) // Stream<String>
.collect(Collectors.toList());
since the IntStream
originally is a sequence of primitive int-values elements.
Another variant of performing such an operation would be :
var s = IntStream.rangeClosed(1, 5)
.boxed()
.map(a -> Integer.toString(a))
.collect(Collectors.toList());
add a comment |
Alternatively, you could use IntStream.boxed
as :
var s = IntStream.rangeClosed(1, 5) // IntStream
.boxed() // Stream<Integer>
.map(String::valueOf) // Stream<String>
.collect(Collectors.toList());
since the IntStream
originally is a sequence of primitive int-values elements.
Another variant of performing such an operation would be :
var s = IntStream.rangeClosed(1, 5)
.boxed()
.map(a -> Integer.toString(a))
.collect(Collectors.toList());
add a comment |
Alternatively, you could use IntStream.boxed
as :
var s = IntStream.rangeClosed(1, 5) // IntStream
.boxed() // Stream<Integer>
.map(String::valueOf) // Stream<String>
.collect(Collectors.toList());
since the IntStream
originally is a sequence of primitive int-values elements.
Another variant of performing such an operation would be :
var s = IntStream.rangeClosed(1, 5)
.boxed()
.map(a -> Integer.toString(a))
.collect(Collectors.toList());
Alternatively, you could use IntStream.boxed
as :
var s = IntStream.rangeClosed(1, 5) // IntStream
.boxed() // Stream<Integer>
.map(String::valueOf) // Stream<String>
.collect(Collectors.toList());
since the IntStream
originally is a sequence of primitive int-values elements.
Another variant of performing such an operation would be :
var s = IntStream.rangeClosed(1, 5)
.boxed()
.map(a -> Integer.toString(a))
.collect(Collectors.toList());
edited 2 days ago
answered 2 days ago
nullpointer
42.8k1091175
42.8k1091175
add a comment |
add a comment |
1
Note that this post is tagged java-8, but the
var
keyword is available from Java 9.– MC Emperor
2 days ago
2
@MCEmperor (true) Java-10 precisely. Anyway, that's not of utmost importance for the current question and can be ignored unless someone actually tries to copy the same code to make it work under Java-8.
– nullpointer
2 days ago