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

Primitive Type Specializations of BinaryOperator – Functional-Style Programming

Primitive Type Specializations of BinaryOperator<T> Table 13.10 shows that the BinaryOperator<T> interface has three primitive type specializations to int, long, and double. The specializations are named PrimBinaryOperator, where Prim is either an Int, Long, or Double (Table 13.10). These primitive type binary operators have the functional method applyAsPrim: (primitive, primitive) -> primitive, where primitive is … Read morePrimitive Type Specializations of BinaryOperator – Functional-Style Programming

Composing Predicates – Functional-Style Programming

Composing Predicates The predicate interfaces define default methods to compose compound predicates—that is, to chain together predicates with logical AND and OR operations. Click here to view code image default Predicate<T> negate() Returns a predicate that represents the logical negation of this predicate. Click here to view code image default Predicate<T> and(Predicate<? super T> other) … Read moreComposing Predicates – Functional-Style Programming

Consumers – Functional-Style Programming

13.7 Consumers The Consumer<T> functional interface represents a consumer of values. From Table 13.6, we see that its functional method accept() has the type T -> void—that is, it takes an argument of type T and returns no value (void). Typically, it performs some operation on its argument object. Table 13.6 shows all the consumer … Read moreConsumers – 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

Accessing Local Variables in the Enclosing Context – Functional-Style Programming

Accessing Local Variables in the Enclosing Context All variable declarations in a lambda expression follow the rules of block scope. They are not accessible outside the lambda expression. It also means that we cannot redeclare local variables already declared in the enclosing scope. In Example 13.2, redeclaring the parameter banner and the local variable words … Read moreAccessing Local Variables in the Enclosing Context – Functional-Style Programming

Composing Consumers – Functional-Style Programming

Composing Consumers The method andThen() can be used to chain together consumers to compose compound consumers. The three consumers used earlier to resize, reverse, and print a StringBuilder can be chained together as seen here: Click here to view code image resizeSB.andThen(reverseSB)    .andThen(printSB).accept(new StringBuilder(“Banana”)); // StringBuilder: anaB The constituent consumers are executed one after the … Read moreComposing Consumers – 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

Composing Functions – Functional-Style Programming

Composing Functions Both the default methods compose() and andThen() of the Function<T, R> interface return an instance of a Function that is created from the caller function (i.e., the function on which the method is invoked) and the argument function (i.e., the function that is passed as an argument to the method). The two methods … Read moreComposing Functions – Functional-Style Programming