如果我在流上执行多个操作,它们是如何在内部计算的。
List<Integer> ee = new ArrayList<Integer>();
Function<? super Integer, ? extends Integer> f1 = x -> x * 2;
Function<? super Integer, ? extends Integer> f2 = x -> x * x;
Function<? super Integer, ? extends Integer> f3 = x -> x / 2;
ee.stream().map(f1.compose(f2.andThen(f3))).collect(Collectors.toList());
ee.stream().map(f1).map(f2).map(f3).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
编辑问题以添加多个地图操作。在多个地图函数应用的情况下如何计算地图函数。在谈论非并行流时,元素的计算顺序是否仅取决于输入集合的类型(即对于列表、linkedHashmap、sortedset 和无序对于 hashSet 等)。另外,我能否对流的内部工作有更多的了解,以便更好地决定何时不建议使用流以及何时建议使用最多。(集合大小、序列的性质等)
Scenario1
When a new user clicks on sign up page
And provides login ID
Then user is signed up and can view profile page.
Scenario2
When user clicks on the edit profile page
And updates his address
Then updated profile should be visible to user
Run Code Online (Sandbox Code Playgroud)
这些场景以相同的顺序写入功能文件中。当为其编写黄瓜文件时,我在场景 1 中创建一个用户。在场景 2 中,正在更新同一用户。在某种程度上,场景 2 依赖于场景 1,因为它正在更新场景 1 中创建的同一用户。
我的问题是创建的场景是否应该依赖于其他场景。或者它们应该独立于彼此的执行。在这种情况下,我应该在场景2中创建一个新用户,然后对其执行和更新并断言它。
我试图在Integer流上使用reduce方法来获取所有值的总和。但出现语法错误,无法找出错误。
List<Integer> ee = new ArrayList<Integer>();
Function<? super Integer, ? extends Integer> f3 = x -> x / 2;
BinaryOperator<? extends Integer> accumulator = (x, y) -> x + y;
ee.stream().map(f3).reduce(new Integer(0), accumulator);
Run Code Online (Sandbox Code Playgroud)
它给出了错误:
The method reduce(capture#2-of ? extends Integer, BinaryOperator<capture#2-of ? extends Integer>) in the type Stream<capture#2-of ? extends Integer> is not applicable for the arguments (Integer, BinaryOperator<capture#7-of ? extends Integer>)
Run Code Online (Sandbox Code Playgroud)