[Solved] For Loop with Lambda Expression in JAVA


I think the reason is pretty clear.

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});

Translates approximately to:

for (Integer i : ints) {
    System.out.println(ints.get(i - 1) + " ");
}

Which will cause IndexOutOfBoundsExceptions because i refers to the elements of each list, and each of those elements – 1 will give an index that is clearly out of bounds. For your first example, i will be 21, which gives an index of 21 - 1 == 20, which is out of bounds for the list you created.

Example:

List<Integer> ints = Stream.of(21,22,32,42,52).collect(Collectors.toList());

will end up so that

ints == [21, 22, 32, 42, 52]

So when you run this:

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});

The computer takes the first element and tries to execute the body of the lambda:

Execute System.out.print(ints.get(i-1) + " ");:
    First element is 21
    21 - 1 == 20
    ints.get(20) --> IndexOutOfBoundsException

And for your second example:

List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());

becomes

ints == [2, 8, 7, 4, 3]

So when you run this:

ints.forEach((i) -> {
    System.out.print(ints.get(i-1) + " ");
});

The computer goes through the elements and tries to execute the body of the lambda:

Execute System.out.print(ints.get(i-1) + " ");:
    First element is 2
    2 - 1 == 1
    ints.get(1) --> 8
    Print 8
Execute System.out.print(ints.get(i-1) + " ");:
    Second element is 8
    8 - 1 == 7
    ints.get(7) --> IndexOutOfBoundsException

So evidently the code in your second example is not what you actually have. I suspect that the code you actually have is:

List<Integer> ints = Stream.of(2,8,7,4,3).collect(Collectors.toList());
System.out.print("the list: ");
ints.forEach((i) -> {
    System.out.print(i + " ");
                     ^^^^^^^ <-- this is different
});

Which is entirely different than what you posted.

16

solved For Loop with Lambda Expression in JAVA