如何使用 Lambda 表达式 .reduce() 方法减少给定列表 [英] How to reduce given list by using Lambda expression .reduce() method

查看:19
本文介绍了如何使用 Lambda 表达式 .reduce() 方法减少给定列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

List<Integer> integers = Arrays.asList(1, 2, 3, 5, 6, 8, 9, 10);
integers.stream().filter((integer) -> integer % 2 == 0).collect(Collectors.toList());

如上图integers是一个List,我们只需要从中过滤偶数即可.我可以通过使用 .filter() 方法来实现.但是,是否有可能使用 .reduce() 方法实现相同的效果.希望,.reduce() 方法通过执行给定的 BynaryOperation 过滤掉所有其他元素并返回缩减列表.

As shown above integers is a List, from which we need to filter only even numbers. I can achieve by using .filter() method. But, is there any possibility to achieve the same with .reduce() method. Hope, .reduce() method filtered out all the other elements by performing given BynaryOperation and return reduced list.

如果我对 .reduce() 方法的理解不正确,请告诉我这个方法到底是做什么的.

If my understanding is not correct on .reduce() method, please let me know what exactly this method do.

推荐答案

你对还原的理解是错误的.reduce 将对所有元素重复应用一个函数以获得一个结果.

Your understanding of reduction is wrong. reduce will apply a function on all elements repeatedly to get one single result.

你似乎认为reduce喜欢做

You seem to think of reduce like doing

1, 2, 3, 5, 6, 8, 9, 10
│  │  │  │  │  │  │  │
└op┘  └op┘  └op┘  └op┘
  │     │     │     │
    result list

事实上,确实如此

1, 2, 3, 5, 6, 8, 9, 10
│  │  │  │  │  │  │  │
└op┘  └op┘  └op┘  └op┘
  │    │      │    │
  └─op─┘      └─op─┘
     │          │
     └────op────┘
           │
   final result value

尽管这是一个概念性的观点,但未指定确切的操作顺序.顺序执行将类似于 (((1 op 2) op 3) op 4)... 而并行执行将是像上面的树这样的执行和部分顺序执行的混合.

Though, this is a conceptional view, the exact order of operations is unspecified. A sequential execution will be like (((1 op 2) op 3) op 4)… while a parallel execution will be a mixture of an execution like the tree above and partial sequential execution(s).

如果您首先将每个元素转换为 List,然后使用连接每个列表的列表操作,您可以滥用 reduce 来创建结果列表,但是,有这有两个问题:

You can abuse reduce to create a result list if you first convert each element into a List and then use a list operation which concatenates each list, however, there are two problems with this:

  • 它没有提供所需的跳过(原始列表的)第二个元素"的逻辑;如果您查看上面的树,应该会清楚,不可能制定正确的 op 函数来在所有可能的执行场景中执行此操作
  • 创建临时列表并将它们连接起来效率很低
  • It doesn’t provide the desired "skip each second element (of the original list)" logic; if you look at the tree above, it should become clear, that it is impossible to formulate a correct op function which does that in every possible execution scenario
  • creating temporary lists and concatenating them is very inefficient

后一点可以通过使用 collect 来解决,这是一个 可变 减少,因此,允许您使用可变列表,您可以向其中添加项目,但是,它没有解决第一点,包括所需的过滤器会违反合同并且只能在顺序执行中工作.

The latter point can be solved by using collect, which is a mutable reduction, hence, allows you to use mutable lists to which you can add items, however, it does not address the first point, including the desired filter would violate the contract and only work in a sequential execution.

所以解决方案是为源列表范围内的所有元素定义一个filter,然后使用collect进行可变归约以创建结果列表,并且,大惊喜,这正是您的原始代码所做的:

So the solution is to define a filter for all elements in the scope of the source list, followed by a mutable reduction to create the result list using collect, and, big surprise, that’s exactly what you original code does:

… .filter(integer -> integer % 2 == 0).collect(Collectors.toList());

这篇关于如何使用 Lambda 表达式 .reduce() 方法减少给定列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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