gpt4 book ai didi

java - 如何过滤 Java 8 流中的内部 Set?

转载 作者:行者123 更新时间:2023-11-30 11:09:51 26 4
gpt4 key购买 nike

我有对象列表

 List<FrameworkAdminLeftMenu> menu = getMenuFromDB();

每个 FrameworkAdminLeftMenu 对象都有方法

public Set<FrameworkAdminLeftMenuCategories> getFrameworkAdminLeftMenuCategorieses() {
return this.frameworkAdminLeftMenuCategorieses;
}

和方法

public String getCssClassName() {
return this.cssClassName;
}

每个 FrameworkAdminLeftMenuCategories 对象都有方法

public Integer getId() {
return this.id;
}

如何通过 getId(1) 过滤所有 List 和 Set 以获取 FrameworkAdminLeftMenuCategories 对象?

例如,类似

 List<FrameworkAdminLeftMenu> collect = menu.stream()
.filter(
f -> f
.getCssClassName()
.contains("adm-content")
)
.collect(Collectors.toList());

List<FrameworkAdminLeftMenuCategories> categs = collect
.stream()
.filter(
f -> f.
getFrameworkAdminLeftMenuCategorieses()
.stream()
.filter(c -> c.getId() == 1)
)
.collect(Collectors.toList());

最佳答案

如果我对这个问题的理解正确,您想要聚合所有集合中的类别并过滤具有正确 ID 的类别。在这种情况下,您应该使用 flatMap .

尝试这样的事情(显然未经测试):

 List<FrameworkAdminLeftMenuCategories> categs = menu.stream()
.filter(f -> f.getCssClassName().contains("adm-content"))
.flatMap(f -> f.getFrameworkAdminLeftMenuCategorieses().stream())
.filter(c -> c.getId() == 1)
.collect(Collectors.toList());

关于java - 如何过滤 Java 8 流中的内部 Set?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27986611/

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