要筛选列表的Java流?
如何在Java8流中使用过滤器过滤出复杂的对象列表。 假设我有一节这样的课要筛选列表的Java流?,java,java-stream,Java,Java Stream,如何在Java8流中使用过滤器过滤出复杂的对象列表。 假设我有一节这样的课 public class InfoLite{ private String right; List<Others> options; private String new; } //getter,setter and constructor List<InfoLite> newInfo = new ArrayList<>(); List<Others&
public class InfoLite{
private String right;
List<Others> options;
private String new;
}
//getter,setter and constructor
List<InfoLite> newInfo = new ArrayList<>();
List<Others> options = new ArrayList<>();
options.add(new Others(1,"game"))
options.add(new Others(2,"unit"))
List<Others> options2 = new ArrayList<>();
options.add(new Others(1,"console"))
List<Others> options3 = new ArrayList<>();
options.add(new Others(2,"zebvra"))
List<Others> options4 = new ArrayList<>();
options.add(new Others(2,"lon"))
info.add(new Test("game,unit",options,"Florida"));
info.add(new Test("console",options2,"Florida"));
info.add(new Test("zebvra",options3,"Florida"));
info.add(new Test("lon",options4,"Florida"));
我有一个类似于此的信息对象
List<InfoLite> info = new ArrayList<>();
List<Others> options = new ArrayList<>();
options.add(new Others(1,"game"))
options.add(new Others(2,"unit"))
options.add(new Others(3,"dest"))
List<Others> options2 = new ArrayList<>();
options.add(new Others(1,"console"))
options.add(new Others(2,"unit"))
List<Others> options3 = new ArrayList<>();
options.add(new Others(1,"zan"))
options.add(new Others(2,"zebvra"))
List<Others> options4 = new ArrayList<>();
options.add(new Others(1,"zan"))
options.add(new Others(2,"lon"))
options.add(new Others(3,"car"))
info.add(new Test("game,unit",options,"Florida"));
info.add(new Test("console",options2,"Florida"));
info.add(new Test("zebvra",options3,"Florida"));
info.add(new Test("lon",options4,"Florida"));
这里基本上过滤了Others
对象,即仅过滤Others
列表中具有right
中匹配值的对象。如果info
中的right
是逗号分隔的,则拆分right
并签入每个Others
列表对象并返回值
我试过的就到此为止,但剩下的我不知道下一步该怎么办
info.stream()
.filter(option -> Arrays.stream(option.getRight().split(",")).collect(Collectors.toList()).contains(option.getOptions().stream().filter(Others :: getWork))))``
假设,您打算打印与InfoLite
的right
值中的一个字符串相匹配的所有Others
实例
如果是这种情况,下面的代码片段应该会有所帮助
List<Others> options2 = new ArrayList<>();
options.add(new Others(1, "console"));
System.out.println(info.stream()
.flatMap(infoLite->infoLite.getOptions()
.stream()
.filter(option->Arrays.stream(infoLite.getRight())
.拆分(“,”)
.collect(收集器.toSet())
.contains(option.getWork()))
.collect(Collectors.toSet());
输出:
[Others{workId=1, work='console'}, Others{workId=2, work='zebvra'}, Others{workId=1, work='game'}, Others{workId=2, work='unit'}, Others{workId=2, work='lon'}]
注意:我在请求程序中做了一些更改
- 在下面的片段中
List<Others> options2 = new ArrayList<>();
options.add(new Others(1, "console"));
更新
要检索完整的InfoList
实例,需要修改代码段
info.forEach(infoLite -> {
Set<String> rightSet = Arrays.stream(infoLite.getRight()
.split(","))
.collect(Collectors.toSet());
infoLite.getOptions()
.removeIf(option -> !rightSet.contains(option.getWork()));
});
info.forEach(System.out::println);
info.forEach(infoLite -> {
Set<String> rightSet = Arrays.stream(infoLite.getRight()
.split(","))
.collect(Collectors.toSet());
infoLite.getOptions()
.removeIf(option -> !rightSet.contains(option.getWork()));
});
info.forEach(System.out::println);
InfoLite{right='game,unit', newString='Florida', options=[Others{workId=1, work='game'}, Others{workId=2, work='unit'}]}
InfoLite{right='console', newString='Florida', options=[Others{workId=1, work='console'}]}
InfoLite{right='zebvra', newString='Florida', options=[Others{workId=2, work='zebvra'}]}
InfoLite{right='lon', newString='Florida', options=[Others{workId=2, work='lon'}]}