You can compute the threshold ahead of time and reference it directly in the filter function.

(Below are 2 examples, depending on whether you like lambdas or not)

final int threshold =computeThreshold(); temperatureStream.filter(new 
FilterFunction<Integer>() {
   @Override public boolean filter(Integer temperature) {
      return temperature >threshold; }
});

final int threshold =computeThreshold(); temperatureStream.filter(temperature -> 
temperature >threshold);


On 08/10/2019 12:46, Komal Mariam wrote:
Hi everyone,

Suppose I have to compute a filter condition

Integer threshold = compute threshold();

If I:

temperatureStream.filter(new FilterFunction<temperature>() {
@Override
public boolean filter(Integer temperature) throws Exception {
Integer threshold = compute threshold();
return temperature > threshold
}

would this mean I have computed threshold over and over again, for every new element in the stream?

my threshold does not changes once it computed. I don't want to recompute it every time for new elements? is there way I can pass it as a parameter to the filter function?


Reply via email to