Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

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. Is it possible to implement an if condition ?

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. One ComboBox for DOY, one for GSAT and one for DURATION.

I want wo filter with 0 up to 3 filter. 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. 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.

question from:https://stackoverflow.com/questions/65885617/if-condition-for-filter-in-java-8-stream

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. It is kind of like a Builder (e.g. 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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...