Lambda Expressions versus Anonymous Classes – Functional-Style Programming

Lambda Expressions versus Anonymous Classes Implementation A lambda expression can only be used to provide implementation of exactly one functional interface. It represents an anonymous function. Unlike an object, it has only behavior and no state. An anonymous class is restricted to either implementing one interface or extending one class, but it is not restricted … Read moreLambda Expressions versus Anonymous Classes – 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 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 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

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

Predicates – Functional-Style Programming

13.6 Predicates The Predicate<T> interface should be familiar by now, having been used earlier in Example 13.1, Example 13.2, and Example 13.3. The Predicate<T> interface defines a boolean-valued function in terms of an instance of its type parameter T. From Table 13.5, we see that its functional method test() has the type T -> boolean—that … Read morePredicates – Functional-Style Programming

Lambda Expressions and Anonymous Classes – Functional-Style Programming

13.3 Lambda Expressions and Anonymous Classes As we have seen in this chapter so far, both anonymous classes and lambda expressions can be used to provide implementation of functional interfaces. Example 13.3 illustrates using both anonymous classes and lambda expressions for this purpose. A common operation on elements in a collection is to select those … Read moreLambda Expressions and Anonymous Classes – 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