What is wrong in my approach to create a List of Integers using IntStream and forEach?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
The naive code I have is:
class $
public static void main(String _)
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> i++).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
where my expectation was to see the following in the console:
[0, 1, 2, 3, 4]
but the actual is:
[0, 0, 0, 0, 0]
Probably something very simple but what am I missing?
java java-8 java-stream
add a comment |Â
up vote
6
down vote
favorite
The naive code I have is:
class $
public static void main(String _)
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> i++).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
where my expectation was to see the following in the console:
[0, 1, 2, 3, 4]
but the actual is:
[0, 0, 0, 0, 0]
Probably something very simple but what am I missing?
java java-8 java-stream
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
The naive code I have is:
class $
public static void main(String _)
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> i++).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
where my expectation was to see the following in the console:
[0, 1, 2, 3, 4]
but the actual is:
[0, 0, 0, 0, 0]
Probably something very simple but what am I missing?
java java-8 java-stream
The naive code I have is:
class $
public static void main(String _)
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> i++).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
where my expectation was to see the following in the console:
[0, 1, 2, 3, 4]
but the actual is:
[0, 0, 0, 0, 0]
Probably something very simple but what am I missing?
java java-8 java-stream
java java-8 java-stream
asked 4 hours ago
Koray Tugay
8,04526108210
8,04526108210
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
4
down vote
accepted
You need to return the incremented value. You postfix incremented a local variable and returned the non-incremented value. Use ++i
not i++
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
add a comment |Â
up vote
7
down vote
i++
has the value of i
before it's incremented. You need to use the prefix operator.
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
Actually, don't do that. There's no reason to mutate i
, it's simply thrown away. Use the side effect free version. That's the whole idea behind functional programming, after all: avoiding side effects.
IntStream.iterate(0, i -> i + 1).limit(5).forEach(val -> ints.add(val));
It would also be better to collect the stream into a list rather than adding values with forEach
. It directly expresses the intention to create a list and again avoids side effects.
final List<Integer> ints = IntStream.iterate(0, i -> i + 1).limit(5)
.boxed().collect(Collectors.toList());
add a comment |Â
up vote
2
down vote
You are using a postfix i++
, instead of a prefix ++i
while passing on the value to forEach
. Changing to the following shall provide you the expected output :
IntStream.iterate(0, i -> ++i).limit(5).forEach(ints::add);
Aside, an alternate way of iterating and combining limit with Java9+ is using IntStream.iterate
with an IntPredicate
as :
IntStream.iterate(0, i -> i < 5, i -> i + 1).forEach(ints::add);
add a comment |Â
up vote
1
down vote
You could use a print to see what was happend:
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return i++;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
In that case, the value of i always will be 0, because the increment occurs after the value is returned, the correct way is
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return ++i;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You need to return the incremented value. You postfix incremented a local variable and returned the non-incremented value. Use ++i
not i++
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
add a comment |Â
up vote
4
down vote
accepted
You need to return the incremented value. You postfix incremented a local variable and returned the non-incremented value. Use ++i
not i++
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You need to return the incremented value. You postfix incremented a local variable and returned the non-incremented value. Use ++i
not i++
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
You need to return the incremented value. You postfix incremented a local variable and returned the non-incremented value. Use ++i
not i++
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
System.out.println(ints);
answered 4 hours ago
flakes
5,80611848
5,80611848
add a comment |Â
add a comment |Â
up vote
7
down vote
i++
has the value of i
before it's incremented. You need to use the prefix operator.
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
Actually, don't do that. There's no reason to mutate i
, it's simply thrown away. Use the side effect free version. That's the whole idea behind functional programming, after all: avoiding side effects.
IntStream.iterate(0, i -> i + 1).limit(5).forEach(val -> ints.add(val));
It would also be better to collect the stream into a list rather than adding values with forEach
. It directly expresses the intention to create a list and again avoids side effects.
final List<Integer> ints = IntStream.iterate(0, i -> i + 1).limit(5)
.boxed().collect(Collectors.toList());
add a comment |Â
up vote
7
down vote
i++
has the value of i
before it's incremented. You need to use the prefix operator.
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
Actually, don't do that. There's no reason to mutate i
, it's simply thrown away. Use the side effect free version. That's the whole idea behind functional programming, after all: avoiding side effects.
IntStream.iterate(0, i -> i + 1).limit(5).forEach(val -> ints.add(val));
It would also be better to collect the stream into a list rather than adding values with forEach
. It directly expresses the intention to create a list and again avoids side effects.
final List<Integer> ints = IntStream.iterate(0, i -> i + 1).limit(5)
.boxed().collect(Collectors.toList());
add a comment |Â
up vote
7
down vote
up vote
7
down vote
i++
has the value of i
before it's incremented. You need to use the prefix operator.
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
Actually, don't do that. There's no reason to mutate i
, it's simply thrown away. Use the side effect free version. That's the whole idea behind functional programming, after all: avoiding side effects.
IntStream.iterate(0, i -> i + 1).limit(5).forEach(val -> ints.add(val));
It would also be better to collect the stream into a list rather than adding values with forEach
. It directly expresses the intention to create a list and again avoids side effects.
final List<Integer> ints = IntStream.iterate(0, i -> i + 1).limit(5)
.boxed().collect(Collectors.toList());
i++
has the value of i
before it's incremented. You need to use the prefix operator.
IntStream.iterate(0, i -> ++i).limit(5).forEach(val -> ints.add(val));
Actually, don't do that. There's no reason to mutate i
, it's simply thrown away. Use the side effect free version. That's the whole idea behind functional programming, after all: avoiding side effects.
IntStream.iterate(0, i -> i + 1).limit(5).forEach(val -> ints.add(val));
It would also be better to collect the stream into a list rather than adding values with forEach
. It directly expresses the intention to create a list and again avoids side effects.
final List<Integer> ints = IntStream.iterate(0, i -> i + 1).limit(5)
.boxed().collect(Collectors.toList());
edited 3 hours ago
answered 4 hours ago
John Kugelman
236k51394447
236k51394447
add a comment |Â
add a comment |Â
up vote
2
down vote
You are using a postfix i++
, instead of a prefix ++i
while passing on the value to forEach
. Changing to the following shall provide you the expected output :
IntStream.iterate(0, i -> ++i).limit(5).forEach(ints::add);
Aside, an alternate way of iterating and combining limit with Java9+ is using IntStream.iterate
with an IntPredicate
as :
IntStream.iterate(0, i -> i < 5, i -> i + 1).forEach(ints::add);
add a comment |Â
up vote
2
down vote
You are using a postfix i++
, instead of a prefix ++i
while passing on the value to forEach
. Changing to the following shall provide you the expected output :
IntStream.iterate(0, i -> ++i).limit(5).forEach(ints::add);
Aside, an alternate way of iterating and combining limit with Java9+ is using IntStream.iterate
with an IntPredicate
as :
IntStream.iterate(0, i -> i < 5, i -> i + 1).forEach(ints::add);
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You are using a postfix i++
, instead of a prefix ++i
while passing on the value to forEach
. Changing to the following shall provide you the expected output :
IntStream.iterate(0, i -> ++i).limit(5).forEach(ints::add);
Aside, an alternate way of iterating and combining limit with Java9+ is using IntStream.iterate
with an IntPredicate
as :
IntStream.iterate(0, i -> i < 5, i -> i + 1).forEach(ints::add);
You are using a postfix i++
, instead of a prefix ++i
while passing on the value to forEach
. Changing to the following shall provide you the expected output :
IntStream.iterate(0, i -> ++i).limit(5).forEach(ints::add);
Aside, an alternate way of iterating and combining limit with Java9+ is using IntStream.iterate
with an IntPredicate
as :
IntStream.iterate(0, i -> i < 5, i -> i + 1).forEach(ints::add);
edited 4 hours ago
answered 4 hours ago
nullpointer
33.7k1068136
33.7k1068136
add a comment |Â
add a comment |Â
up vote
1
down vote
You could use a print to see what was happend:
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return i++;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
In that case, the value of i always will be 0, because the increment occurs after the value is returned, the correct way is
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return ++i;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
add a comment |Â
up vote
1
down vote
You could use a print to see what was happend:
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return i++;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
In that case, the value of i always will be 0, because the increment occurs after the value is returned, the correct way is
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return ++i;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You could use a print to see what was happend:
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return i++;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
In that case, the value of i always will be 0, because the increment occurs after the value is returned, the correct way is
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return ++i;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
You could use a print to see what was happend:
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return i++;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
In that case, the value of i always will be 0, because the increment occurs after the value is returned, the correct way is
final List<Integer> ints = new ArrayList<>();
IntStream.iterate(0, i ->
System.out.println(i);
return ++i;
).limit(5)
.forEach(val -> ints.add(val));
System.out.println(ints);
answered 4 hours ago
Damian Lattenero
8,86821642
8,86821642
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53182741%2fwhat-is-wrong-in-my-approach-to-create-a-list-of-integers-using-intstream-and-fo%23new-answer', 'question_page');
);
Post as a guest
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
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
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