1z0-809 Oracle Java SE 8 Programmer II – Lambda Built-in Functional Interfaces
January 27, 2023

1. 4.1 Use the built-in interfaces included in the java.util.function package such

Hello everyone. In this lesson we will focus on the first item of the lambda builtin functional interfaces topic in the Java Se eight programmer two exam syllabus that is using the builtin interfaces included in the Java Util function package such as predicate, consumer function and supplier. You have known about functional interfaces when going through lambda expressions. Let’s review these interfaces before diving deeper into some particular types of the Java Util function package. Functional interfaces provide target types for lambda expressions and method references. They are used when declaring contexts that expect a function. You can think of functional interfaces as placeholders for lambda expressions. A functional interface has a single abstract method whose parameters and return type are matched by those of the lambda expression in the using context.

The word match over here implies that parameter types of the lambda expression and those of the abstract method are the same and the return type of the lambda expression is the same as or a subtype of the abstract methods return type. All types included in the Java Util function package are functional interfaces. This lesson focuses on four basic types of the package including predicate, consumer function and supplier. The predicate interface represents a boolean valued function of an argument. The unique abstract method of this interface is named Test, which evaluates the containing predicate on the given argument. The predicate interface defines three default methods which enables predicates to be combined. Those methods are negate and and, or. A default method has the same functionality as the logical operator of the same name. Here is a class that will be used to demonstrate the predicate function pay attention to the git, gender and get age methods as they will be used to construct our predicates. This class denotes applicants for a military service, so gender and age are important factors. This is a method that has a predicate parameter.

This predicate checks whether the given applicant is valid. If such an applicant is, an acceptance message is printed on the console. Otherwise, another message announcing the rejection is output. The first predicate in the main method is simple checking if an applicant is male. The second is another basic predicate that verifies the applicant’s age. The third predicate is composite, combining the first two using the logical and operator. This composite predicate is then used to verify applicants based on property values of the given applicants. These messages are printed out. The consumer interface represents an operation that works on an input argument and does not return a value. Unlike most other functional interfaces, consumer operates via side effects. The abstract method of the consumer interface is Accept, which applies the containing consumer to the given argument. The consumer interface has a default method that allows you to combine multiple consumers. That default method is named and then which returns a consumer composed of the containing consumer object and the operation denoted by the method argument.

This is the applicant class. You have seen with some modifications. This class will be used in the following example that features the consumer interface. Here is a method making use of a consumer for the purpose of demonstration. This method only applies such a consumer to an applicant and does nothing else. The first consumer set the applicant’s status to accepted if the age property is greater than or equals to 18 and less than 60. The second prints a notification if the applicant is accepted. Those consumers are combined when passed to the handle applicant method using the and then method. Here are the results when such composite consumers operate on the given applicant objects. The function interface represents a function that accepts an argument and produces a result. The single abstract method of the function interface is apply which operates on the given argument. The type of the argument is the first type argument when declaring the function object, while the return type is specified by the second type argument. The function interface defines two default methods to chain function objects namely And Then and Compose. The behavior of these methods is opposite and Then applies this function first, then the function argument compose applies the function argument first, then applies this function to the result. This is the applicant class used for our next example. It is the exact same as the class and the consumer example and shown here.

Just for your quick reference. Here is an example illustrating how you can use a function. This is the handle applicant method which invokes the function argument on the specified applicant then outputs the result. This is the first function that changes the status property of a valid applicant. This function applies to an object of type applicant and returns the same object with possible modifications. So the return type is the same. Here is the second function which returns a string accepted or rejected depending on the status property of the input applicant. Notice that the type of the input argument to a function is specified by the first type argument to the function declaration, whereas the return type is denoted by the second type argument. Those functions can be combined using the compose and and then methods. Here are the results when combined functions are passed to the handle applicant method. The supplier interface represents a supplier of objects of a particular type. The return to objects may or may not be the same across different invocations. This interface has a single abstract method called git. The return type of this method is specified by the type argument when declaring a supplier function. This interface does not have any default methods.

This is a class that will be used to illustrate operations of a supplier function. The most noticeable member of this applicant class is its constructor. The use of a random instance over here is to generate a random ID for a new applicant object. It is not required to do so and you can return the same object or different objects with the same internal state from different invocations of a supplier. Here is a supplier declaration which does nothing but invoke the applicant constructor. Applicant objects returned from the supplier in this case are not different from what you would have gotten by calling the constructor. In a more complicated situation, a supplier enables you to implement some extra jobs before returning a new object. This interface is also handy when you want to work with streams. Now it’s time for some practice questions to go over what we have learned. Here is the first question. As always, feel free to pause the video if you need more time. The predicate over here tests if the input number is even. This predicate is combined with a predicate of the opposite functionality using the logical or operator. The combined predicate therefore evaluates all expressions to true. This predicate is then negated, meaning that the final composed predicate filters out all stream elements. As a result, the correct answer is D. Now for the second question.

An instance method can only be invoked on a particular object instead of an expression like this. As a result, this statement fails to be compiled, so the correct answer is D. If a consumer variable is declared to refer to our method reference like this option B would have been the correct answer. Now for the third question. When declaring a function variable like this and this the first type argument is the type of the input object, whereas the second type argument is the return type of the function, not the other way around. So the apply method over here expects an integer instead of a string, and this statement fails to be compiled. Likewise, the integer class does not define a length method, so this statement cannot be compiled either. So the correct answer is D. If the type arguments to the function declarations are switched like here and here, option B would have been the correct answer. Now for the fourth question. This supplier expects a return value of type enum, while its function creates an instance of enum provider. All enum types are subtypes of the enum class, so the given declaration is valid and the correct answer is A. In this lesson we have gone over four basic functional interfaces and the Java util function package. Here is a quick summary the predicate interface represents a Boolean valued function of an argument. The consumer interface represents an operation that accepts a single input argument and returns no result. The function interface represents a function that accepts an argument and produces a result. The supplier interface represents a supplier of objects.

2. 4.2 Develop code that uses primitive versions of functional interfaces

Hello everyone. In this lesson we will go over the second item of the lambda builtin Functional Interfaces topic and the Java Se Eight programmer two exam syllabus that is developing code that uses primitive versions of functional interfaces. Generic functional interfaces can only operate on reference types. When using these generic interfaces with primitive types, values are autoboxed and unboxed incurring overhead at both compile time and runtime during compilation. The compiler must implement an extra job converting variables between primitive types and primitive wrapper classes. Meanwhile, handling objects of wrapper classes at Runtime is more expensive than working directly with primitive values. The Java Util function package provides primitive type specializations of functional interfaces. Those specializations support the primitive types int, long and double. In case of supplier, an additional interface is provided to work with the boolean type. You may declare custom functional interfaces to work with other primitive types, but this is not required in the exam. A primitive functional interface has a similar name to that of a generic one, except for the prefix identifying its supported primitive type. The name of the abstract method of a primitive interface is also akin to, if not the same as that of the corresponding generic type. There are three primitive versions of the predicate interface. They are int predicate, long predicate, and double predicate. The name of the abstract method of primitive predicate interfaces is the same as that of the generic predicate. The only difference in the signature of such a method is the parameter type instead of an object reference type.

The parameter type of a primitive functional method is a primitive data type. The letter x over here represents the primitive type int long or double depending on the containing interface. Like the generic predicate interface, a primitive predicate interface has three default methods negate and or. The x prefix in the parameter and return types over here is the placeholder for int long or double depending on the enclosing interface. The main difference between primitive functional interfaces and interfaces working on primitive wrapper classes is the involvement of autoboxing and unboxing. This example clearly demonstrates such a difference. In the first predicate, which is a primitive function, the I parameter is of the primitive type int, not a reference type, so this statement cannot be compiled. On the contrary, the second predicate is a generic function operating on a primitive wrapper class. No matter the argument to this function at runtime is a primitive value or an integer object. Its parameter is always an integer object. So this statement always prints true when the containing predicate is executed. Like primitive predicate interfaces, there are three primitive versions of consumer interfaces end consumer, long consumer, and double consumer.

The name of the abstract method of primitive consumer interfaces is the same as that of the generic consumer. Its parameter type is, however, different. Rather than an object reference, the parameter of a primitive consumer function is a primitive value. This letter x denotes the primitive type int long or double depending on the functional interface and use. As you may have expected, a primitive consumer interface has a default method with the same name as that of the generic consumer. The x letter here and here embodies a prefix into long or double depending on the associated interface. This example illustrates the difference between a primitive consumer interface and the generic one when it comes to invocations of methods on the consumer’s parameter.

The parameter of a primitive function is of a primitive type, so a method defined in the double wrapper class cannot be invoked on that parameter. The same method should run well in a generic consumer function. Unlike the interface’s predicate consumer and supplier, a generic function interface has two type arguments, one for the parameter and one for the return value. To facilitate function operations on primitive values, various primitive versions of the function interface have been introduced which can be grouped into three categories. The first kind of primitive version is functions that accept a primitive argument and return an object. The abstract method name in this case is no different from the method of the generic function interface.

The second kind is functions that take an object argument and return a primitive value. The name of the abstract method is a bit different in this case where a suffix expressing the functions return type is appended. The last kind is functions that accept a primitive argument and return a value of another primitive type. The abstract method name is similar to the name and the second function kind with a suffix implying the functions return type. Note that unlike the generic function interface, primitive function interfaces do not define default methods. Here is an example illustrating different kinds of primitive functions. This function takes a long primitive value and returns a string object. This accepts a string object and returns an int primitive, whereas the last function takes a double primitive and returns a long value. The supplier interface has four primitive versions including int supplier, long supplier, double supplier, and boolean.

Supplier primitive supplier interfaces have a single abstract method and no default methods the same as the generic supplier interface. The name of that unique method is a bit different, however. Instead of git, the abstract method of a primitive interface is called git as x with the letter x denoting the return type of the generated value which is int, long, double, and boolean. Here is a primitive supplier demonstration. With this supplier declaration, the maximum value of the long primitive type is printed out when this statement is executed. Now it’s time for some practice questions to go over what we have learned. Here is the first question. As always, feel free to pause the video if you need more time. There is nothing wrong with this code fragment. First a predicate is created, then it is negated. At this point, the result of the negation is applied to the number zero. The original predicate evaluates to true with such an argument, but the negation operator flips it to false, so the correct answer is B. Now for the second question. The consumer over here accepts a long primitive value, while this method reference can take a value of any type.

Therefore this assignment is completely valid. Our consumer is then combined with itself. The combination will print the input argument twice. This literal is an integer not long, but will be cast too long when it is passed to the consumer. Notice that the casting can be implemented because the functional interface is a primitive version. If it were a generic interface with the type argument long, the compilation would have failed, since an int primitive value cannot be autoboxed to a long wrapper class instance. So the correct answer is B. Now for the third question. This function converts a string to integer, while the function over here increases the integer parameter by one and converts the result to a string. In this statement, the string zero is passed to the first function and the result is passed to the second function which produces the textual representation of number one. All of these statements are then valid.

Therefore, option D it prints the string one is the correct answer. One thing to note here is that the name of the functional method which is apply as followed by a suffix denoting the return type. This is the fourth question. The lambda expression in this situation does not take a parameter and returns a double value, so it can be used as a double supplier. The given code fragment is valid, so this statement prints a random double value which was generated from this expression. So option C is the correct answer. Notice that the supplier in this scenario is a primitive functional method, so its name is git as double, not git. In this lesson, we have gone over primitive versions of functional interfaces. Let’s take a look at the summary. Generic functional interfaces can only operate on reference types when using primitive types. With these generic interfaces, values are auto boxed and unboxed. Incurring overhead at both compile time and runtime. The Java util function package provides primitive type specializations of functional interfaces. Those specializations support the primitive types int long, double and boolean.

3. 4.3 Develop code that uses binary versions of functional interfaces

Hello everyone. In this lesson we will cover the third item of the lambda built in Functional interfaces topic and the Java Se eight programmer two exam syllabus that is developing code that uses binary versions of functional interfaces. Binary functional interfaces are functional interfaces that have an abstract method with two parameters. These interfaces have similar semantics to their corresponding one arity functional interfaces. All basic functional interfaces except for supplier have binary versions. The reason for the lack of binary specializations of supplier is simple it does not have any parameter. The predicate interface has only one binary specialization, which is by predicate. The abstract method of bipredicate has the same name as that of other predicate functional interfaces. This method has two parameters. The types of these parameters are specified by type arguments when declaring a bipredicate instance. The by predicate interface defines three default methods, namely negate and an or. These methods have the same semantics as those of the predicate interface. This is a bipredicate instance that simply compares an integer and a double. With these arguments, the buy predicate evaluates to false, so this string is printed out.

The consumer interface has four binary specializations including by consumer objectint, consumer object long, consumer, and object double. Consumer abstract methods of binary consumer interfaces have the same name as those of other consumer functional interfaces. They are different only in the parameter types. The by consumer function has two object parameters, while an objectx consumer has an object reference and a primitive parameter. The letter x here and here represents a primitive type which might be int long or double. The by consumer interface defines a default method named and then which has the same semantics as that of the consumer interface. Here is an example illustrating operations of binary consumer interfaces. The first consumer has two parameters of the string type. This consumer concatenates the input strings with a dot character in between. When these arguments are passed in, the stringwhizlabs. com is printed on the console.

This is another binary consumer. The second parameter of this function is of the int primitive type which can be implied from the name of the functional interface. The type of the first parameter is determined by this type argument with a string argument of twelve characters. The execution of this method will print out the number eight. The function interface has four binary specializations including by function, two int by function, two long by function and two double by function. In fact, function has four other specializations related to the binary operator interface.

Those binary interfaces will be covered shortly. Abstract methods of binary function interfaces have either of these signatures. In the by function interface, the abstract method is named apply, taking two object parameters and returning another object. The types of parameters and the return object are specified by type arguments to the function. In the functions that return a primitive value, the parameters are also objects. The type of the return value can be inferred from the method name as well as the interface name. The by function interface declares a default method named and then which has the same semantics as that of the function interface. Here are two classes used for our example on binary function interfaces. Both classes have the same structure with a name and an age property. The name property is of type string, while the age is an int primitive value. Here is how the male and female classes are used with binary function interfaces. This function takes two arguments.

of types male and female. It returns a concatenation of their name as such, when the print line statement over here is executed, this string is printed in this function. The return type is not specified by a type argument as it has already been known as double. While the parameters are of types male and female, the function body calculates the average age of the input male and female instances. The result of the calculation is then printed out by this statement. The binary operator interface is a specialization of by function, representing an operation upon two operands of the same type and producing a result of the same type as the operands. Since all input and output values are of the same type, this interface has only one type parameter instead of three as the by function interface. Binary operator is a subinterface of by function, so it inherits all members of the by function interface, including abstract and default methods. The binary operator interface has three primitive type specializations including int binary operator, long binary operator and double. Binary operator abstract methods of binary operator interfaces are similar to those of binary function interfaces. You can see the signature of such abstract methods over here. In this example, the first binary operator takes two string arguments and returns another string constructed from the input.

When this operator applies to these arguments, the string wizlabs. com is printed in the second operator. Both parameters and the return value are of the primitive type long. Two input values are added up and the result is returned. Applying this operator to these long literals, you will get the number one. Now it’s time for some practice questions to go over what we have learned. Here is the first question. As always, feel free to pause the video if you need more time. The code fragment in this situation is completely valid, so there are no compilation errors. The predicate over here combines with the opposite of itself using the or operator, so the resulting predicate always evaluates to true regardless of argument values. So the correct answer is A. Now for the second question. The consumer over here takes two arguments of types person and string. Its function body sets the name of the person argument to the string input value. Such a change will be reflected in the object passed to the consumer, so the correct answer is A. Now for the third question.

Among the three type arguments to the buy function interface. In this case, the first two represent parameter types, while the last is the return type. These type arguments do not match the definition of this lambda expression, where the person constructor expects two string arguments rather than a person and a string. This mismatch results in a compilation error, so the correct answer is C. If type arguments to the bifurcation interface were changed like this option, A would have been the correct answer. Now for the fourth question. This binary operator works on objects of the wrapper class double. It can also accept arguments of the double primitive type thanks to autoboxing, but not any other primitive type. The arguments over here are of type int, so they cannot be passed to the given operator. As a result, the correct answer is C. In this lesson, we have gone over binary versions of functional interfaces. To sum up, binary functional interfaces are functional interfaces that have an abstract method with two parameters. All basic functional interfaces except for supplier have corresponding binary versions.

4. 4.4 Develop code that uses the UnaryOperator interface

Hello everyone. In this lesson we will go through the last item of the Lambda builtin Functional Interfaces topic and the Java Se Eight programmer two exam syllabus that is developing code that uses the UNAIR operator interface. The uner operator interface is a special specialization of function representing an operation upon a single operand and producing a result of the same type as the operend. Since the types of input and output values are the same, this interface has only one type parameter instead of two as the function interface. Unary operator is a subinterface of function, so it inherits all members of the function interface, including abstract and default methods. The single abstract methods of uniry operator is the same as that of the function interface, except for the requirement that the parameter type of the operator must be the same as its return type. Here is a class that will be used to illustrate the unery operator interface in the following example. An instance of this person class has two properties name and age. These are getter methods of those properties. This unary operator takes a person instance, then constructs a new person based on that input object. The type of the operator’s argument is specified by this type argument over here. A person object is created, then passed to our operator. The operator returns a new person instance. At this point, property values of that new object are printed on the console.

Now it’s time for some practice questions to go over what we have learned. Here is the first question. As always, feel free to pause the video if you need more time. The argument to this unary operator must be a double, either a primitive value or a wrapper class instance. The actual argument at runtime over here is an integer literal which is not accepted by the operator. As such, this statement fails to be compiled and the correct answer is D. Now for the second question. The replace all method of the list interface expects a uniry operator instance like this, so there are no compilation errors. In this situation, the replace all method replaces each element of the list it is invoked on with the result of the past n operator. Therefore, this string java in lowercase will be replaced by its uppercase version, so the correct answer is B. In this lesson we have learned how to use the unary operator interface. In short, the unary operator interface is a specialization of function representing an operation upon a single operand and producing a result of the same type as the operand.

Unary operator is a subinterest interface of function, so it inherits all members of the function interface, including abstract and default methods.

Leave a Reply

How It Works

img
Step 1. Choose Exam
on ExamLabs
Download IT Exams Questions & Answers
img
Step 2. Open Exam with
Avanset Exam Simulator
Press here to download VCE Exam Simulator that simulates real exam environment
img
Step 3. Study
& Pass
IT Exams Anywhere, Anytime!