Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

foreach - for loop using lambda expression in JAVA

My Code:

List<Integer> ints = Stream.of(1,2,4,3,5).collect(Collectors.toList());
ints.forEach((i)-> System.out.print(ints.get(i-1)+ " "));

out put:

1 2 3 4 5

my question is why i must be i-1 inside the get method? does i-1 prevent the out of boundary issue?

Does below code acts like the for loop iteration?

(i)-> System.out.print(ints.get(i-1))

so is above code equal to this

for(Ineger i:ints)
   System.out.print(ints.get(i));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The lambda parameter i takes the value of the items in the collection, not the indexes. You are subtracting 1 because the values happen to be one greater than their index.

If you tried with

List<Integer> ints = Stream.of(10,20,40,30,50).collect(Collectors.toList());
ints.forEach((i)-> System.out.print(ints.get(i-1)+ " "));

You would find the code does not work so well.

You should be able to simply do (not needing to do a get call)

ints.forEach((i)-> System.out.print(i + " "));

Your lambda and your proposed for loop are not equivalent.

ints.forEach((i)-> System.out.print(ints.get(i-1)))

Would be equivalent to

for(Integer i:ints)
   System.out.print(ints.get(i-1));

Note the preservation of the minus 1.


In response to the comment:

Lambdas are not loops, they are functions (effectively anyway). In your first example the forEach method is what provides the looping functionality. The argument lambda is what it should do on each iteration. This is equivalent to the body of your for loop

In the example in the comment, max is the function that provides the loop like behavior. It will iterate (do a loop) of the items to find the maximum value). The lambda you provide i -> i would be an identity function. It takes one parameter and returns that object unmodified.

Suppose you had a more complex object and you wanted to compare them on a particular member such as GetHighScore(). Then you could use i -> i.GetHighScore() to get the object with the highest score.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...