IntStream rangeClosed unable to return value other than int
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
add a comment |
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
add a comment |
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
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
java lambda java-8 java-stream
edited 38 mins ago
Nicholas K
5,60151031
5,60151031
asked 41 mins ago
Julez Jupiter
164110
164110
add a comment |
add a comment |
5 Answers
5
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.
mapToObj
allows you to map an int
value to a reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
.
Thank you. It works like a charm.
– Julez Jupiter
39 mins ago
add a comment |
Use mapToObj instead :
IntStream.rangeClosed(1, 5).mapToObj(String::valueOf).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());
2
causes unnecessary overhead,mapToObj
is the idiomatic approach and the way to go.
– Aomine
34 mins ago
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.
So, let's go through the relevant stream pipeline operations:
IntStream.range
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endExclusive (exclusive) by an incremental step of 1.
map
returns 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 |
17/12/18 9:50 p. m. - Ivan: PTT-20181217-WA0003.opus (archivo adjunto) 18/12/18 12:46 p. m. - Ivan: Me encantas😍😍😍😍😍😍😍 18/12/18 12:46 p. m. - Ivan: Mi amor 18/12/18 12:47 p. m. - Ivan: Holaaaaaa 18/12/18 12:47 p. m. - Ivan: Chikita 18/12/18 12:47 p. m. - Ivan: Ey 18/12/18 12:47 p. m. - Ivan: Preciosa 21/12/18 12:07 a. m. - Ivan: Ey 21/12/18 12:10 a. m. - Ivan: . 21/12/18 1:13 a. m. - Amor😍: Que 21/12/18 1:22 a. m. - Ivan: Envés de que ables con migo 21/12/18 1:22 a. m. - Amor😍: Apenas me conecte 💁🏻♀ 21/12/18 1:22 a. m. - Ivan: Pues si y envés de ablar con migo ablas con otro bato 21/12/18 1:23 a. m. - Amor😍: No
New contributor
Voting to delete as spam.
– nullpointer
17 mins ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984363%2fintstream-rangeclosed-unable-to-return-value-other-than-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
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.
mapToObj
allows you to map an int
value to a reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
.
Thank you. It works like a charm.
– Julez Jupiter
39 mins ago
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.
mapToObj
allows you to map an int
value to a reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
.
Thank you. It works like a charm.
– Julez Jupiter
39 mins ago
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.
mapToObj
allows you to map an int
value to a reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
.
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.
mapToObj
allows you to map an int
value to a reference type, and thus transform the IntStream
to a Stream<SomeReferenceType>
.
edited 38 mins ago
answered 40 mins ago
Eran
279k37449535
279k37449535
Thank you. It works like a charm.
– Julez Jupiter
39 mins ago
add a comment |
Thank you. It works like a charm.
– Julez Jupiter
39 mins ago
Thank you. It works like a charm.
– Julez Jupiter
39 mins ago
Thank you. It works like a charm.
– Julez Jupiter
39 mins ago
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 40 mins ago
Nicholas K
5,60151031
5,60151031
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());
2
causes unnecessary overhead,mapToObj
is the idiomatic approach and the way to go.
– Aomine
34 mins ago
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());
2
causes unnecessary overhead,mapToObj
is the idiomatic approach and the way to go.
– Aomine
34 mins ago
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 mins ago
answered 37 mins ago
nullpointer
42.1k1089175
42.1k1089175
2
causes unnecessary overhead,mapToObj
is the idiomatic approach and the way to go.
– Aomine
34 mins ago
add a comment |
2
causes unnecessary overhead,mapToObj
is the idiomatic approach and the way to go.
– Aomine
34 mins ago
2
2
causes unnecessary overhead,
mapToObj
is the idiomatic approach and the way to go.– Aomine
34 mins ago
causes unnecessary overhead,
mapToObj
is the idiomatic approach and the way to go.– Aomine
34 mins ago
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.
So, let's go through the relevant stream pipeline operations:
IntStream.range
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endExclusive (exclusive) by an incremental step of 1.
map
returns 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.
So, let's go through the relevant stream pipeline operations:
IntStream.range
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endExclusive (exclusive) by an incremental step of 1.
map
returns 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.
So, let's go through the relevant stream pipeline operations:
IntStream.range
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endExclusive (exclusive) by an incremental step of 1.
map
returns 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.
So, let's go through the relevant stream pipeline operations:
IntStream.range
returns an IntStream
as per the documentation:
Returns a sequential ordered IntStream from startInclusive (inclusive)
to endExclusive (exclusive) by an incremental step of 1.
map
returns 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
.
answered 7 mins ago
Aomine
39.4k73669
39.4k73669
add a comment |
add a comment |
17/12/18 9:50 p. m. - Ivan: PTT-20181217-WA0003.opus (archivo adjunto) 18/12/18 12:46 p. m. - Ivan: Me encantas😍😍😍😍😍😍😍 18/12/18 12:46 p. m. - Ivan: Mi amor 18/12/18 12:47 p. m. - Ivan: Holaaaaaa 18/12/18 12:47 p. m. - Ivan: Chikita 18/12/18 12:47 p. m. - Ivan: Ey 18/12/18 12:47 p. m. - Ivan: Preciosa 21/12/18 12:07 a. m. - Ivan: Ey 21/12/18 12:10 a. m. - Ivan: . 21/12/18 1:13 a. m. - Amor😍: Que 21/12/18 1:22 a. m. - Ivan: Envés de que ables con migo 21/12/18 1:22 a. m. - Amor😍: Apenas me conecte 💁🏻♀ 21/12/18 1:22 a. m. - Ivan: Pues si y envés de ablar con migo ablas con otro bato 21/12/18 1:23 a. m. - Amor😍: No
New contributor
Voting to delete as spam.
– nullpointer
17 mins ago
add a comment |
17/12/18 9:50 p. m. - Ivan: PTT-20181217-WA0003.opus (archivo adjunto) 18/12/18 12:46 p. m. - Ivan: Me encantas😍😍😍😍😍😍😍 18/12/18 12:46 p. m. - Ivan: Mi amor 18/12/18 12:47 p. m. - Ivan: Holaaaaaa 18/12/18 12:47 p. m. - Ivan: Chikita 18/12/18 12:47 p. m. - Ivan: Ey 18/12/18 12:47 p. m. - Ivan: Preciosa 21/12/18 12:07 a. m. - Ivan: Ey 21/12/18 12:10 a. m. - Ivan: . 21/12/18 1:13 a. m. - Amor😍: Que 21/12/18 1:22 a. m. - Ivan: Envés de que ables con migo 21/12/18 1:22 a. m. - Amor😍: Apenas me conecte 💁🏻♀ 21/12/18 1:22 a. m. - Ivan: Pues si y envés de ablar con migo ablas con otro bato 21/12/18 1:23 a. m. - Amor😍: No
New contributor
Voting to delete as spam.
– nullpointer
17 mins ago
add a comment |
17/12/18 9:50 p. m. - Ivan: PTT-20181217-WA0003.opus (archivo adjunto) 18/12/18 12:46 p. m. - Ivan: Me encantas😍😍😍😍😍😍😍 18/12/18 12:46 p. m. - Ivan: Mi amor 18/12/18 12:47 p. m. - Ivan: Holaaaaaa 18/12/18 12:47 p. m. - Ivan: Chikita 18/12/18 12:47 p. m. - Ivan: Ey 18/12/18 12:47 p. m. - Ivan: Preciosa 21/12/18 12:07 a. m. - Ivan: Ey 21/12/18 12:10 a. m. - Ivan: . 21/12/18 1:13 a. m. - Amor😍: Que 21/12/18 1:22 a. m. - Ivan: Envés de que ables con migo 21/12/18 1:22 a. m. - Amor😍: Apenas me conecte 💁🏻♀ 21/12/18 1:22 a. m. - Ivan: Pues si y envés de ablar con migo ablas con otro bato 21/12/18 1:23 a. m. - Amor😍: No
New contributor
17/12/18 9:50 p. m. - Ivan: PTT-20181217-WA0003.opus (archivo adjunto) 18/12/18 12:46 p. m. - Ivan: Me encantas😍😍😍😍😍😍😍 18/12/18 12:46 p. m. - Ivan: Mi amor 18/12/18 12:47 p. m. - Ivan: Holaaaaaa 18/12/18 12:47 p. m. - Ivan: Chikita 18/12/18 12:47 p. m. - Ivan: Ey 18/12/18 12:47 p. m. - Ivan: Preciosa 21/12/18 12:07 a. m. - Ivan: Ey 21/12/18 12:10 a. m. - Ivan: . 21/12/18 1:13 a. m. - Amor😍: Que 21/12/18 1:22 a. m. - Ivan: Envés de que ables con migo 21/12/18 1:22 a. m. - Amor😍: Apenas me conecte 💁🏻♀ 21/12/18 1:22 a. m. - Ivan: Pues si y envés de ablar con migo ablas con otro bato 21/12/18 1:23 a. m. - Amor😍: No
New contributor
New contributor
answered 18 mins ago
Edgar Ivan Vivas
11
11
New contributor
New contributor
Voting to delete as spam.
– nullpointer
17 mins ago
add a comment |
Voting to delete as spam.
– nullpointer
17 mins ago
Voting to delete as spam.
– nullpointer
17 mins ago
Voting to delete as spam.
– nullpointer
17 mins ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984363%2fintstream-rangeclosed-unable-to-return-value-other-than-int%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown