如何过滤Java8流中的内部集合?
我有一个对象列表如何过滤Java8流中的内部集合?,java,java-stream,Java,Java Stream,我有一个对象列表 List<FrameworkAdminLeftMenu> menu = getMenuFromDB(); 方法 public String getCssClassName() { return this.cssClassName; } 每个FrameworkAdminLeftMenuCategories对象都有方法 public Set<FrameworkAdminLeftMenuCategories> getFrameworkAd
List<FrameworkAdminLeftMenu> menu = getMenuFromDB();
方法
public String getCssClassName() {
return this.cssClassName;
}
每个FrameworkAdminLeftMenuCategories
对象都有方法
public Set<FrameworkAdminLeftMenuCategories> getFrameworkAdminLeftMenuCategorieses() {
return this.frameworkAdminLeftMenuCategorieses;
}
public Integer getId() {
return this.id;
}
如何通过getId(1)
筛选所有列表并设置为获取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());
List collect=menu.stream()
.过滤器(
f->f
.getCssClassName()
.包含(“adm内容”)
)
.collect(Collectors.toList());
列表类别=收集
.stream()
.过滤器(
f->f。
GetFrameworkAdminLeftMenuCategories()
.stream()
.filter(c->c.getId()==1)
)
.collect(Collectors.toList());
如果我正确理解了这个问题,您希望聚合所有集合中的类别,并筛选具有正确ID的类别。在这种情况下,您应该使用
试试这样(显然未经测试):
List categs=menu.stream()
.filter(f->f.getCssClassName().contains(“adm内容”))
.flatMap(f->f.GetFrameworkAdminLeftMenuCategories().stream())
.filter(c->c.getId()==1)
.collect(Collectors.toList());
“categories”复数的复数形式?:-)不完全确定你在问什么。是否要从具有该ID的所有集合中收集所有类别?在这种情况下,请在.flatMap(f->f.getFrameworkAdminLeftMenuCategorises())上尝试flatMap
不可编译的代码(Function@ArthurKhusntudinov我想我错过了.stream()
在flatMap
行末尾的一个.stream()
。现在可以用了吗?太棒了!非常感谢!tobias_k,你说的“分类”是什么意思“复数的复数形式”?我的实体结构不正确吗?@Arthurkhunstudinov不,我只是在这个词上费解了一下,似乎那些类别
对象聚合了多个类别(因为“类别”是“类别”的复数形式),因此“类别”的集合
因此是“类别”,即复数的复数“;-)
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());