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

Method and Constructor References – Functional-Style Programming

13.13 Method and Constructor References So far we have seen that a Java program can use primitive values, objects (including arrays), and lambda expressions. In this section, we introduce the fourth kind of value that a Java program can use, called method references (and their specializations to constructors and array construction). As we shall see, … Read moreMethod and Constructor References – Functional-Style Programming

Accessing Members in an Enclosing Class – Functional-Style Programming

Accessing Members in an Enclosing Class Just like nested blocks, a lambda expression has lexical or block scope—that is, names used in a lambda expression are resolved lexically in the local context in which the lambda expression is defined. A lambda expression does not inherit names of members declared in the functional interface it implements, … Read moreAccessing Members in an Enclosing Class – Functional-Style Programming

Primitive Type Specializations of Predicate – Functional-Style Programming

Primitive Type Specializations of Predicate<T> The functional interfaces IntPredicate, LongPredicate, and DoublePredicate evaluate predicates with int, long, and double arguments, respectively, avoiding the overhead of boxing and unboxing of primitive values (see Table 13.5). The primitive type versions are not subinterfaces of the Predicate<T> interface. Click here to view code image Predicate<Integer> isEven = i … Read morePrimitive Type Specializations of Predicate – Functional-Style Programming

Primitive Type Specializations of BiFunction – Functional-Style Programming

Primitive Type Specializations of BiFunction<T, U, R> Table 13.8 shows that the BiFunction<T, U, R> interface has three primitive type twoarity generic specializations to int, long, and double, but they do not define any default methods for creating compound functions. The specializations are named ToPrimBiFunction<T, U>, where Prim is either Int, Long, or Double. These … Read morePrimitive Type Specializations of BiFunction – 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 BiConsumer – Functional-Style Programming

Primitive Type Specializations of BiConsumer<T, U> Table 13.6 shows the generic functional interfaces ObjIntConsumer<T>, ObjLongConsumer<T>, and ObjDoubleConsumer<T> that are specializations of the BiConsumer<T, U> interface. The functional method accept() of these primitive type specializations takes two arguments: One is an object of type T and the other is a primitive value. These functional interfaces are … Read morePrimitive Type Specializations of BiConsumer – 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

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