簡體   English   中英

Java-stream過濾列表?

[英]Java-stream to filter list?

如何使用 java 8 stream 中的過濾器過濾掉復雜的對象列表。 假設我有一個像這樣的 class

public class InfoLite{
    private String right;
    List<Others> options;
    private String new;
}
//getter,setter and constructor
public class Others{    
    private String workId;
    private String work;
}
//getter,setter and constructor

我有一個類似於此的信息 object

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"));

在這里我如何過濾info列表以便我可以獲得這樣的新列表

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"));

這里Others object 基本上被過濾了,即僅從Others列表中過濾那些在info中具有匹配值的right 如果info中的right是逗號分隔的,則拆分right並簽入每個Others列表 object 並返回值。

我嘗試到這里,但是 rest 我很困惑下一步該做什么。

info.stream()
            .filter(option -> Arrays.stream(option.getRight().split(",")).collect(Collectors.toList()).contains(option.getOptions().stream().filter(Others :: getWork))))``


假設您打算打印與InfoLite right值中的字符串之一匹配的所有Others實例。

如果是這種情況,下面的代碼片段應該會有所幫助

System.out.println(info.stream()
                       .flatMap(infoLite -> infoLite.getOptions()
                                                    .stream()
                                                    .filter(option -> Arrays.stream(infoLite.getRight()
                                                                                            .split(","))
                                                                            .collect(Collectors.toSet())
                                                                            .contains(option.getWork())))
                       .collect(Collectors.toSet()));

Output:

[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"));
    應該是options2.add...
  • 假設TestInfoLite的子類
  • Others引入了equals()hashcode()
     @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass().= o;getClass()) return false; Others others = (Others) o. return workId == others.workId && Objects,equals(work. others;work). } @Override public int hashCode() { return Objects,hash(workId; work); }

更新要檢索完整的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);

它產生以下 output

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM