for循环在JAVA中使用lambda表达式 [英] for loop using lambda expression in JAVA

查看:2495
本文介绍了for循环在JAVA中使用lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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

输出:


1 2 3 4 5

我的问题是为什么我必须是i-1里面的get方法? I-1是否防止出现边界问题?



下面的代码是否像for循环迭代一样?

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

所以上面的代码等于(Ineger I:ints)
System.out.print(ints.get(i));


解决方案

lambda参数 i 获取集合中的项目的值,而不是索引。您正在减去 1 ,因为这些值碰巧比它们的索引大1。



p>

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

您会发现代码无法正常工作。



你应该可以简单地做(不需要做一个 get 调用)

<$ p $ ints.forEach((i) - > System.out.print(i +));






您的lambda和您建议的for循环并不等同。

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

相当于

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

请注意保存的减号1.



< hr>

回应评论:

Lambdas不是循环,它们是函数(有效地)。在你的第一个例子中, forEach 方法提供了循环功能。 lambda参数是每次迭代时应该做的。这相当于for循环的



在注释示例中, max 是提供类似循环行为的函数。它将迭代(做一个循环)的项目来找到最大值)。您提供的 i - >我会是一个身份识别功能。假设你有一个更复杂的对象,你想在一个特定的成员上比较它们,比如 GetHighScore()。那么你可以使用 i - > i.GetHighScore()来获得得分最高的对象。


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));

解决方案

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.

这篇关于for循环在JAVA中使用lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆