'Java 11 Select a method to apply on a stream based on enum value

I have the following enum.

public enum AggregationType
{
    MIN,
    MAX,
    AVERAGE
}

let's assume that i have a function where i pass enum value like

public Float someFunction(final AggregationType enum) {
            return (float) provides.stream()
                                      .mapToDouble(this::someFunc)
                                      .average()
                                      .orElse(-1);
        

}

I want to apply this .average() .min() .max() methods on a stream based on enum value.

How i can achieve this? I don't want to use simply switch function inside someFunction but rather in this return statement.

So i want to have something like

public Float someFunction(final AggregationType enum) {
            return (float) provides.stream()
                                      .mapToDouble(this::someFunc)
                                      .decideWhichMethodShouldBeUsed()
                                      .orElse(-1);


}

where decideWhichMethodShouldBeUsed() decides which func to use based on enum.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source