Scrivio Interview library
DesignIntermediate6 min read · updated 12 November 2026

When should a team define its own functional interface instead of using java.util.function types? Give an example where a custom one is clearly better.

Use the standard java.util.function types for generic plumbing, because they work directly with streams, Optional and other JDK APIs. Define your own functional interface when it adds something the standard ones cannot: a domain name and documented contract, such as PriceRule instead of Function<Order, BigDecimal>; checked exceptions, which standard types cannot declare; or a primitive signature with a domain name where no standard specialisation fits. Agree on conventions so the codebase does not fill up with near-identical interfaces.

Why this question is asked

Every team that writes lambdas eventually faces this choice. Function<Order, BigDecimal> is ready to use and every Java developer recognises it, but it says nothing about what the function means or what rules it must follow. A custom interface can say all of that, at the cost of one more type. Interviewers ask this to see whether we make the choice deliberately, and whether we know the concrete cases where a custom interface is clearly better. In this answer, we will see what standard types do well, then four reasons for a custom interface with code for each, and finish with conventions that keep the codebase consistent.

1What the Standard Types Do Well

The java.util.function package provides general shapes: Function<T, R> maps a T to an R, Predicate<T> tests a T, Supplier<T> produces a value, Consumer<T> accepts one, plus two-argument and primitive variants. Their strength is interoperability. Stream.map takes a Function, Optional.filter takes a Predicate, Map.computeIfAbsent takes a Function, and they all compose with methods like andThen and negate.

So for plumbing code, a generic utility that retries any Supplier, a cache that computes missing values with a Function, a filter in a stream, the standard types are the right choice. Wrapping them in custom names there would only force conversions at every JDK call.

Java
static <T> T withRetry(Supplier<T> call, int attempts) { /* ... */ return call.get(); } // generic: standard type
Map<String, Price> cache = new ConcurrentHashMap<>();
Price p = cache.computeIfAbsent("SKU-1", sku -> client.fetch(sku)); // Function expected

2Reason 1: A Domain Name and a Documented Contract

Compare two signatures for the same concept. A list of Function<Order, BigDecimal> could mean discounts, taxes, shipping costs or anything else that maps an order to a number. A PriceRule interface says what it is, and its Javadoc can state the rules every implementation must follow.

Java
/**
* Computes a discount for an order. Implementations must return a value >= 0,
* must not exceed the order total, and must not call external services.
*/
@FunctionalInterface
public interface PriceRule {
BigDecimal discountFor(Order order);
default PriceRule plus(PriceRule other) { // a domain-specific combinator
return o -> discountFor(o).add(other.discountFor(o));
}
}
PriceRule loyalty = o -> o.total().multiply(new BigDecimal("0.05"));
PriceRule bulk = o -> o.items().size() > 10 ? new BigDecimal("20") : BigDecimal.ZERO;
BigDecimal d = loyalty.plus(bulk).discountFor(order); // 25.0000 for a 100.00 order with 12 items

This is the case where a custom interface is clearly better. Method signatures such as applyRules(List<PriceRule> rules) explain themselves, IDE searches for implementations of PriceRule find every rule in the system, and the contract has a home. A Function cannot carry any of this, and searching for usages of Function finds thousands of unrelated lambdas. Lambdas still work exactly the same, because PriceRule is a functional interface.

3Reason 2: Checked Exceptions

The standard types declare no checked exceptions, so a lambda implementing them cannot throw one. A custom interface can declare them, which lets callers write natural code and handle the exception at the right level. Spring's JDBC RowMapper is a well-known example: its mapRow method declares throws SQLException, so mapping code can call ResultSet methods directly.

Java
@FunctionalInterface
public interface RowReader<T> {
T read(ResultSet rs) throws SQLException; // impossible with Function<ResultSet, T>
}
<T> List<T> query(String sql, RowReader<T> reader) throws SQLException {
try (var st = conn.prepareStatement(sql); var rs = st.executeQuery()) {
List<T> out = new ArrayList<>();
while (rs.next()) out.add(reader.read(rs));
return out;
}
}
List<String> names = query("select name from customer", rs -> rs.getString("name"));

4Reason 3: Primitive Signatures With a Domain Name

The standard set has primitive specialisations, such as LongUnaryOperator and IntPredicate, that avoid boxing an int or long into an object on every call. But they cover only common shapes. There is no standard interface for a function taking a long and an int and returning a long, for example. In hot code that works with amounts in cents, a custom interface gives us both the right primitive signature and a name.

Java
@FunctionalInterface
public interface FeeSchedule {
long feeCents(long amountCents, int daysLate); // no boxing, no standard equivalent
}
FeeSchedule standard = (amount, days) -> days <= 0 ? 0 : Math.min(amount / 100 * days, 5_000);
System.out.println(standard.feeCents(250_000, 3)); // 5000: 7500 capped at 5000
System.out.println(standard.feeCents(40_000, 2)); // 800

When a standard primitive type already fits, such as LongUnaryOperator for a long-to-long adjustment, we can still choose a custom name if the domain meaning matters more than interoperability. That is a judgement call; the next section is about making it consistently.

5Conventions That Keep the Codebase Consistent

The risk of custom interfaces is fragmentation. Without conventions, three teams create Validator, Check and Rule, all equivalent to Predicate<Order>, and code that combines them needs adapters. A few agreed rules prevent this.

Step throughStandard type or custom functional interface?
  1. Is it generic plumbing used with JDK APIs?
    YesUse java.util.functionNoConsider a custom interface
  2. Must it throw a checked exception?
    YesCustom interface with throwsNoCheck the domain
  3. Does it name a domain concept with rules?
    YesCustom interface with JavadocNoUse java.util.function
  4. Does an equivalent already exist in our code?
    YesReuse itNoAdd it in the shared domain package

4 steps in order. Play them, or step through them one at a time.

Custom interfaces need a reason; when one exists, reuse before creating.

In practice the conventions are short. Domain functional interfaces live in the domain package of the module that owns the concept, are annotated with @FunctionalInterface, and are named after the concept, not the shape. Before adding one, we search for an existing equivalent. And where a custom interface must meet a JDK API, method references convert cheaply: rules.stream().map(r -> r.discountFor(order)) or passing rule::discountFor where a Function<Order, BigDecimal> is expected.

6Follow-up questions

Can a custom functional interface be used where a Function is expected?

Not directly, because Java's functional interfaces are nominal: a PriceRule is not a Function even if the shapes match. A method reference converts it in one step, for example rule::discountFor, which creates a Function that calls the rule. That small friction is the main cost of custom interfaces.

Should a custom interface extend Function to get interoperability?

It can, for example interface PriceRule extends Function<Order, BigDecimal>, which makes every rule usable in streams. The downside is that it inherits Function's andThen and compose and its apply name, which may not fit the domain, and the abstract method becomes apply. It is a reasonable choice when interoperability matters more than a clean domain vocabulary.

Are there costs at run time for custom interfaces?

No meaningful ones. Lambdas for custom and standard interfaces are created the same way through invokedynamic, and calls are ordinary interface calls that the JIT optimises equally well. The real cost is only in design: more types to learn.

7Conclusion

In this answer, we saw that java.util.function types are the right default for generic plumbing because they work directly with JDK APIs. A custom functional interface is clearly better when it names a domain concept with a documented contract, like PriceRule, when it must declare checked exceptions, like a row reader throwing SQLException, or when it needs a primitive signature the standard set lacks. Conventions on where these live, how they are named and reusing existing ones keep the codebase from fragmenting.

What to say in the interviewAbout 90 seconds aloud

Open with

My default is the standard java.util.function types for plumbing, because streams, Optional, Map.computeIfAbsent and other JDK APIs take them directly, and every Java developer knows them. A generic retry helper should take a Supplier, not a custom type.

Then, if they want more

I define my own functional interface when it adds something real. The clearest case is domain meaning: instead of Function of Order to BigDecimal, which could be a discount, a tax or anything, I'd have a PriceRule interface with a discountFor method and Javadoc saying it must return a non-negative value, never exceed the total and not call external services.

Signatures become self-explanatory, finding all implementations is easy, and I can add domain combinators like plus. Second, checked exceptions: the standard types can't declare them, so something like Spring's RowMapper, which throws SQLException, needs a custom interface.

Third, primitive signatures: the standard set has common primitive specialisations, but nothing for, say, a long and an int returning a long, so a FeeSchedule interface avoids boxing and names the concept. The risk is fragmentation, three teams each inventing their own version of Predicate of Order.

So I'd agree conventions: domain interfaces live in the owning module's domain package, carry @FunctionalInterface, are named after the concept, and we search for an existing one first. Where they meet JDK APIs, a method reference converts them.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "When should a team define its own functional interface instead of using...": https://scrivio-interview-library.netlify.app/java/interfaces-and-abstract-classes/93faaae4cd/