Overriding Abstract Methods from Multiple Interfaces, Revisited – Functional-Style Programming

Overriding Abstract Methods from Multiple Interfaces, Revisited A general discussion on overriding abstract methods from multiple superinterfaces can be found in §11.12, p. 621. In general, an interface can inherit multiple abstract methods from its superinterfaces, but a functional interface can only have a single abstract method. Note that superinterfaces need not be functional interfaces. … Read moreOverriding Abstract Methods from Multiple Interfaces, Revisited – Functional-Style Programming

Composing Two-Arity Functions – Functional-Style Programming

Composing Two-Arity Functions The interface BiFunction<T, U, R> provides the andThen() method for creating compound two-arity functions. Given a two-arity function f and a one-arity function g, the method evaluates the functions as follows: f.andThen(g).apply(x, y) emulates g.apply(f.apply(x, y))—that is, the two-arity function f is executed first and the one-arity function g last. In the … Read moreComposing Two-Arity Functions – Functional-Style Programming

Lambda Body – Functional-Style Programming

Lambda Body A lambda body is either a single expression or a statement block. Execution of a lambda body either has a non-void return (i.e., its evaluation returns a value), or has a void return (i.e., its evaluation does not return a value), or its evaluation throws an exception. A single-expression lambda body is used … Read moreLambda Body – Functional-Style Programming

Primitive Type Specializations of Supplier – Functional-Style Programming

Primitive Type Specializations of Supplier<T> The primitive type versions of the generic supplier interface are appropriately named with a prefix to indicate the type of primitive value returned by their functional methods. For example, the integer supplier is named IntSupplier. Their functional methods are also appropriately named with a postfix to indicate the type of … Read morePrimitive Type Specializations of Supplier – Functional-Style Programming

Primitive Type Specializations of Function – Functional-Style Programming

Primitive Type Specializations of Function<T, R> As can be seen in Table 13.7, there are three categories of primitive type one-arity specializations of the Function<T, R> interface, each distinguished by a naming scheme. Also, these primitive type one-arity specializations do not define any default methods. These one-arity generic functions have the functional method apply: primitive … Read morePrimitive Type Specializations of Function – Functional-Style Programming

Suppliers – Functional-Style Programming

13.5 Suppliers As the name suggests, the Supplier<T> functional interface represents a supplier of values. From Table 13.4, we see that its functional method get() has the type () -> T—that is, it takes no argument and returns a value of type T. Table 13.4 shows all supplier functional interfaces provided in the java.util.function package. … Read moreSuppliers – Functional-Style Programming

Overview of Built-In Functional Interfaces – Functional-Style Programming

13.4 Overview of Built-In Functional Interfaces Earlier in this chapter, specialized interfaces (including some functional ones) were mentioned that are readily available in the Java SE Platform API (p. 678). To facilitate defining common functions with lambda expressions, the Java SE Platform API also provides a versatile set of functional interfaces for this purpose. The … Read moreOverview of Built-In Functional Interfaces – Functional-Style Programming

Primitive Type Specializations of UnaryOperator – Functional-Style Programming

Primitive Type Specializations of UnaryOperator<T> The UnaryOperator<T> interface has three primitive type specializations to int, long, and double. The specializations are named PrimUnaryOperator, where Prim is either Int, Long, or Double (Table 13.9). These non-generic unary operators have the functional method applyAsPrim: primitive -> primitive, where primitive is an int, long, or double—the operator takes … Read morePrimitive Type Specializations of UnaryOperator – Functional-Style Programming

Type Checking and Execution of Lambda Expressions – Functional-Style Programming

Type Checking and Execution of Lambda Expressions A lambda expression can only be defined in a context where a functional interface can be used: for example, in an assignment context, a method call context, or a cast context (p. 733). The compiler determines the target type that is required in the context where the lambda … Read moreType Checking and Execution of Lambda Expressions – Functional-Style Programming

Filtering Criteria Defined by Anonymous Classes – Functional-Style Programming

Filtering Criteria Defined by Anonymous Classes Example 13.3 uses anonymous classes to instantiate the criteria object, as shown at (1) and (2). The basic idea is that we can both declare and instantiate the class at the same time, where it is needed in the code, and in our case, as an argument in the … Read moreFiltering Criteria Defined by Anonymous Classes – Functional-Style Programming