Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要筛选列表的Java流?_Java_Java Stream - Fatal编程技术网

要筛选列表的Java流?

要筛选列表的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&

如何在Java8流中使用过滤器过滤出复杂的对象列表。 假设我有一节这样的课

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'}]}