Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

When you can change the enum type to

public enum AggregationType {
    MIN(DoubleStream::min),
    MAX(DoubleStream::max),
    AVERAGE(DoubleStream::average);

    public final Function<DoubleStream, OptionalDouble> operation;

    AggregationType(Function<DoubleStream, OptionalDouble> f) {
        operation = f;
    }
}

you can implement the method like

public Float someFunction(final AggregationType aType) {
    return (float)aType.operation.apply(provides.stream().mapToDouble(this::someFunc))
        .orElse(-1);
}

If changing the enum type is not an option, you have to handle the mapping to the actual operation at the place where you want to implement someFunction, e.g.

private static final Map<AggregationType,Function<DoubleStream, OptionalDouble>> OPS;
static {
    EnumMap<AggregationType,Function<DoubleStream, OptionalDouble>>
        m = new EnumMap<>(AggregationType.class);
    m.put(AggregationType.MIN, DoubleStream::min);
    m.put(AggregationType.MAX, DoubleStream::max);
    m.put(AggregationType.AVERAGE, DoubleStream::average);
    OPS = Collections.unmodifiableMap(m);
}

public Float someFunction(final AggregationType aType) {
    return (float)OPS.get(aType).apply(provides.stream().mapToDouble(this::someFunc))
        .orElse(-1);
}

You can also use a switch statement, but it’s rather clunky

public Float someFunction(final AggregationType aType) {
    DoubleStream ds = provides.stream().mapToDouble(this::someFunc);
    OptionalDouble d;
    switch(aType) {
        case MAX: d = ds.max(); break;
        case MIN: d = ds.min(); break;
        case AVERAGE: d = ds.average(); break;
        default: throw new AssertionError();
    }
    return (float)d.orElse(-1);
}

Things get better when you’re using a recent Java version, as then, you can use a switch expression:

public Float someFunction(final AggregationType aType) {
    DoubleStream ds = provides.stream().mapToDouble(this::someFunc);
    return (float)(switch(aType) {
        case MAX -> ds.max();
        case MIN -> ds.min();
        case AVERAGE -> ds.average();
    }).orElse(-1);
}

This is only accepted by the compiler when all enum constants are handled. Then, it will generate an equivalent to default -> throw new AssertionError(); behind the scenes which will never be taken at runtime, as long as no-one changes the enum type after this code has been compiled.

Generally, only the first variant forces developers who consider adding new constants to AggregationType to also also consider handling the associated operation.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!