我正在努力退还仅包含item.baz.fooz =='snafu'的物品。我已经匿名了下面的代码和源。您的协助将不胜感激。 我的数据来源:
{
"data": {
"searches": [
{
"apples": [
{
"pears": [
{
"sets": [
{
"items": [
{
"baz": {
"fooz": {
"unit": "snafu"
}
}
}
]
}
]
}
]
}
]
}
]
}
}
我的失败代码:
List<Item> items =
response.data.searches.stream()
.flatMap(
search -> search.apples.forEach(
apple -> apple.pears.forEach(
pear -> pear.sets.forEach(
set -> set.items.stream()
.filter(item -> item.baz.fooz.unit.equals("snafu"))
.collect(Collectors.toList())))));
这些失败是(其他)失败:
Incompatible type. Required List<Foo> but 'flatmap' was inferred to Stream<R>: no instances of type variable R List<Foo>
答案 0 :(得分:4)
不要使用using Microsoft.Practices.Unity.Configuration;
,您需要多个forEach
:
flatMap
或者(按照霍尔格的建议):
List<Item> snoozles =
response.data
.searches
.stream() // Stream<Search>
.flatMap(search -> search.apples.stream()) // Stream<Apple>
.flatMap(apple -> apple.pears.stream()) // Stream<Pear>
.flatMap(pear -> pear.sets.stream()) // Stream<Set>
.flatMap(set -> set.items.stream()
.filter(item -> item.baz.fooz.unit.equals("snafu"))) // Stream<Item>
.collect(Collectors.toList()); // List<Item>