gpt4 book ai didi

java - Cactoos flatMap类比

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:38 24 4
gpt4 key购买 nike

有没有flatMap类比 Cactoos图书馆?我需要的正是 flatMap 可以做到的,但没有流:

The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream.

例如如果我在列表中有一些值,并且每个值都有子项目,并且我想从每个值中获取所有项目,我可以使用 flatMap:

List<Value> values = someValues();
List<Item> items = values.stream()
.flatMap(val -> val.items().stream()) // val.items() returns List<Item>
.collect(Collectors.toList());

如何使用 Cactoos 而不是流 API 做同样的事情?

最佳答案

您可以使用 Joined ,它相当于展平 Iterable

例如,你会写:

new Joined<>(new Mapped<>(val -> val.items(), someValues()));

关于java - Cactoos flatMap类比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54462337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com