简体   繁体   English

在JAVA中使用lambda表达式的for循环

[英]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 1 2 3 4 5

my question is why i must be i-1 inside the get method?我的问题是为什么在 get 方法中我必须是 i-1? does i-1 prevent the out of boundary issue? i-1 是否可以防止越界问题?

Does below code acts like the for loop iteration?下面的代码是否像 for 循环迭代?

(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. lambda 参数i采用集合中项目的值,而不是索引。 You are subtracting 1 because the values happen to be one greater than their index.您正在减去1因为这些值恰好比它们的索引大 1。

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)你应该能够简单地做(并不需要做一个get调用)

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

Your lambda and your proposed for loop are not equivalent.您的 lambda 和您建议的 for 循环并不等效。

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.注意负 1 的保留。


In response to the comment:回应评论:

Lambdas are not loops, they are functions (effectively anyway). Lambda 不是循环,它们是函数(无论如何都是有效的)。 In your first example the forEach method is what provides the looping functionality.在您的第一个示例中, forEach方法提供了循环功能。 The argument lambda is what it should do on each iteration.参数 lambda 是它在每次迭代中应该做的事情 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.在注释中的示例中, max是提供类似循环行为的函数。 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 .您提供的 lambda i -> i将是一个恒等函数 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() .假设您有一个更复杂的对象,并且您想在特定成员(例如GetHighScore()上比较它们。 Then you could use i -> i.GetHighScore() to get the object with the highest score.然后你可以使用i -> i.GetHighScore()来获得得分最高的对象。

List indexes in Java are 0-based. Java 中的列表索引是从 0 开始的。

Therefore:所以:

ints.get(0) == 1;
ints.get(1) == 2;
ints.get(2) == 3;
//etc...

You're calling ints.get(i-1) for each "i" where "i" is equal to the value of each element in the list "ints".您正在为每个“i”调用 ints.get(i-1),其中“i”等于列表“ints”中每个元素的

If you were to call ints.get(i) you'd be fetching elements with indices equal to 1,2,3,4 and 5 and 5 wouldn't be a valid index into a list with 5 elements.如果您要调用ints.get(i)您将获取索引等于 1,2,3,4 和 5 的元素,而 5 将不是具有 5 个元素的列表的有效索引。


This code:这段代码:

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

is equivalent to:相当于:

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

Your examples aren't equivalent.你的例子是不等价的。

This is a wish, rather than a solution!这是一个愿望,而不是一个解决方案! Other language like NodeJS/JavaScript have a similar loop that passes two arguments to the lambda.其他语言如 NodeJS/JavaScript 有一个类似的循环,将两个参数传递给 lambda。 Parameter i is the index and parameter j is the item from the container (regarless of container type).参数 i 是索引,参数 j 是容器中的项目(无论容器类型如何)。

ints.forEach((i,j)-> System.out.print("Index is ${i} and item is ${j}", i, j)); ints.forEach((i,j)-> System.out.print("Index is ${i} and item is ${j}", i, j));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM