简体   繁体   English

如果过滤条件在 Java 8 stream

[英]if condition for filter in Java 8 stream

I want to use the filter in a stream only if a checkbox is selected or on an selected index > 1 In the example below there is only 1 filter but I want to use more than one. I want to use the filter in a stream only if a checkbox is selected or on an selected index > 1 In the example below there is only 1 filter but I want to use more than one. Is it possible to implement an if condition?是否可以实现 if 条件?

List<SoeEntry> sortedList = soeArraylist.stream()
   .filter(p -> p.getEntryDoy().equals(comboBoxDoys.getSelectedItem()))
   .sorted(
         Comparator
           .comparing(SoeEntry::getEntryGSAT)
           .thenComparing(SoeEntry::getEntryNumber)
          )
   .collect(Collectors.toList());
   sortedList.forEach(System.out::println);

Something like就像是

if(comboBox1.getSelectedIndex() > 1) {.filter(p -> p.getEntryDoy().equals(comboBox1.getSelectedItem()))}
if(comboBox2.getSelectedIndex() > 1) {.filter(p -> p.getEntryGSAT().equals(comboBox2.getSelectedItem()))}
if(comboBox3.getSelectedIndex() > 1) {.filter(p -> p.getEntryDuration().equals(comboBox3.getSelectedItem()))}

EDIT: I will try to explain it again.编辑:我将尝试再次解释它。 Lets say I have 3 ComboBoxes with items.假设我有 3 个带有项目的 ComboBoxes。 One ComboBox for DOY, one for GSAT and one for DURATION.一个 ComboBox 用于 DOY,一个用于 GSAT,一个用于 DURATION。

I want wo filter with 0 up to 3 filter.我想要 0 到 3 个过滤器的 wo 过滤器。 If I select the DOY 025 I want to see only the entries from DOY 025, if I select additionally the GSAT XYZ I want to see the entries from DOY 025 with GSAT XYZ only.如果我 select DOY 025 我只想查看 DOY 025 中的条目,如果我 select 另外 GSAT XYZ 我想查看 DOY 025 中的条目,仅带有 GSAT XYZ。 If I select additionally a DURATION of 30 I want to see the entries from DOY 025 with GSAT XYZ and a DURATION of 30 only.如果我 select 额外的持续时间为 30,我想查看 DOY 025 的条目,GSAT XYZ 和持续时间仅为 30。

Streams themselves are only a description of the action to perform when being collected and are immutable.流本身只是对收集时要执行的操作的描述,并且是不可变的。 Each method on Stream returns a new instance. Stream上的每个方法都返回一个新实例。 It is kind of like a Builder (eg StringBuilder).它有点像 Builder(例如 StringBuilder)。

Stream<SoeEntry> stream = soeArraylist.stream();
if(comboBox1.getSelectedIndex() > 1) {
  stream = stream.filter(p -> p.getEntryDoy().equals(comboBox1.getSelectedItem()));
} else if(comboBox2.getSelectedIndex() > 1) {
  stream = stream.filter(p -> p.getEntryDoy().equals(comboBox2.getSelectedItem()));
} else if(comboBox3.getSelectedIndex() > 1) {
  stream = stream.filter(p -> p.getEntryDoy().equals(comboBox3.getSelectedItem()));
}

 
List<SoeEntry> sortedList = stream
   .sorted(
         Comparator
           .comparing(SoeEntry::getEntryGSAT)
           .thenComparing(SoeEntry::getEntryNumber)
          ))
   .collect(Collectors.toList());
   System.out.println(sortedList);

Alternatively, compute the predicate before and then use it in your stream:或者,先计算谓词,然后在 stream 中使用它:

Predicate<SoeEntry> predicate;
if(comboBox1.getSelectedIndex() > 1) {
  predicate = p -> p.getEntryDoy().equals(comboBox1.getSelectedItem());
} else if(comboBox2.getSelectedIndex() > 1) {
  predicate = p -> p.getEntryDoy().equals(comboBox2.getSelectedItem());
} else if(comboBox3.getSelectedIndex() > 1) {
  predicate = p -> p.getEntryDoy().equals(comboBox3.getSelectedItem());
} else {
  predicate = p -> true;
}
 
List<SoeEntry> sortedList = stream
   .filter(predicate)
   .sorted(
         Comparator
           .comparing(SoeEntry::getEntryGSAT)
           .thenComparing(SoeEntry::getEntryNumber)
          ))
   .collect(Collectors.toList());
   System.out.println(sortedList);

You can first process the comboboxes (assuming the object Combobox ) to create a chain of predicates ( Stream<Predicate<SoeEntry>> ) which can be packed into a single Predicate<SoeEntry> using logical AND with Predicate#and method:您可以首先处理组合框(假设 object Combobox )以创建谓词链( Stream<Predicate<SoeEntry>> ),可以使用逻辑 AND 和Predicate#and方法将其打包到单个Predicate<SoeEntry>中:

static Predicate<SoeEntry> comboboxPredicate(Combobox... comboboxes) {
    return Arrays.stream(comboboxes)
                 .filter(combobox -> combobox.getSelectedIndex() > 1)
                 .map(combobox -> (Predicate<SoeEntry>) soeEntry -> soeEntry
                        .getEntryDay()
                        .equals(combobox.getSelectedItem()))
                 .reduce(Predicate::and)
                 .orElseGet(() -> soeEntry -> true);      // "no" filter by default
}

This can be easily applied:这可以很容易地应用:

Predicate<SoeEntry> predicate = comboboxPredicate(combobox1, combobox2, combobox3);

List<SoeEntry> sortedList = soeArraylist.stream()
   .filter(predicate)
   .sorted(Comparator.comparing(SoeEntry::getEntryGSAT)
                     .thenComparing(SoeEntry::getEntryNumber))
   .collect(Collectors.toList());

Finally I found a solution with a combination of your input.最后,我找到了结合您输入的解决方案。

    Stream<SoeEntry> stream = soeArraylist.stream();
    List<Predicate<SoeEntry>> allPredicates = new ArrayList<Predicate<SoeEntry>>();
    
    if(comboBoxDoys.getItemCount()  > 0 && comboBoxDoys.getSelectedIndex()  > 1) stream = stream.filter(p -> p.getEntryDoy().equals(comboBoxDoys.getSelectedItem()));
    if(comboBoxSoeSC.getItemCount() > 0 && comboBoxSoeSC.getSelectedIndex() > 1) stream = stream.filter(p -> p.getEntryGSAT().contains(comboBoxSoeSC.getSelectedItem().toString()));
 
    if(!chckbxTTCF1.isSelected() && !chckbxTTCF2.isSelected() && !chckbxTTCF3.isSelected() && !chckbxTTCF4.isSelected() && !chckbxTTCF5.isSelected() && !chckbxTTCF6.isSelected()) {
        //allPredicates.add(p -> p.getEntryNumber() > 0);
    } else {
        if(chckbxTTCF1.isSelected()) allPredicates.add(p -> p.getEntryTTCF1().contains(getScHex(comboBoxSoeSC.getSelectedItem().toString()).getScGSAT()));
        if(chckbxTTCF2.isSelected()) allPredicates.add(p -> p.getEntryTTCF2().contains(getScHex(comboBoxSoeSC.getSelectedItem().toString()).getScGSAT()));
        if(chckbxTTCF3.isSelected()) allPredicates.add(p -> p.getEntryTTCF3().contains(getScHex(comboBoxSoeSC.getSelectedItem().toString()).getScGSAT()));
        if(chckbxTTCF4.isSelected()) allPredicates.add(p -> p.getEntryTTCF4().contains(getScHex(comboBoxSoeSC.getSelectedItem().toString()).getScGSAT()));
        if(chckbxTTCF5.isSelected()) allPredicates.add(p -> p.getEntryTTCF5().contains(getScHex(comboBoxSoeSC.getSelectedItem().toString()).getScGSAT()));
        if(chckbxTTCF6.isSelected()) allPredicates.add(p -> p.getEntryTTCF6().contains(getScHex(comboBoxSoeSC.getSelectedItem().toString()).getScGSAT()));
        
        stream = stream.filter(allPredicates.stream().reduce(x->false, Predicate::or));
    }
     
    List<SoeEntry> sortedList = stream
       .sorted(
             Comparator
               .comparing(SoeEntry::getEntryNumber)
              // .thenComparing(SoeEntry::getEntryNumber)
              )
       .collect(Collectors.toList());
       //System.out.println(sortedList);
       sortedList.forEach(System.out::println);

Many thx to all:)非常感谢所有人:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM