1z0-809 Oracle Java SE 8 Programmer II – Generics and Collections
January 27, 2023

7. 3.7 Filter a collection by using lambda expressions

Hello everyone. In this lesson, we will focus on the 7th item of the Generics and Collections topic and the JavaScript programmer two exam syllabus that is, filtering a collection by using lambda expressions. You can filter elements of a collection using the Remove If method. This is a default method of the collection interface, thus inherited by all of its implementation classes. The Remove If method takes a predicate argument and removes all elements of the collection that satisfy the specified predicate. This method returns a Boolean value, indicating whether any elements were removed. Since predicate is a functional interface, you can use a lambda expression as the argument to the Remove If method. The parameter type of that lambda expression is the type of collection elements.

Here is the person class whose instances will be used in our example illustrating the filtering process on a collection. This class has three properties name, gender and age. These statements create a collection and populate it with four person instances. When this collection is filtered for the first time, all persons whose ages are greater than 25 are removed. This statement then prints the name of the remaining objects Emma and Noah, whose ages are 20 and 24 respectively. In the second filtering, all persons who are male are filtered out. This means there is only one person instance left after both filters apply, that person is Emma, whose age is not greater than 25 and whose gender is not male.

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. When a collection is created with a raw type, its elements are considered to be of type object. Hence, the type of the lambda expression parameter is also object. Despite the actual type of collection elements at Runtime being data, the object type does not define a gitvar method.

Therefore, the given code fragment fails to be compiled and the correct answer is D. If a correct type argument were to be specified, like this option, a would have been the correct answer. Now for the second question. The Remove If method over here removes all even numbers. However, whether or not a number is removed from the collection, it always gets printed. As a result, all elements of the collection in this scenario are printed out, so the correct answer is C. In this lesson, we have gone over the process of filtering a collection using lambda expressions. Here is a quick summary. You can filter elements of a collection using the Remove If method, which takes a predicate argument and removes all elements of the collection that satisfy the specified predicate. You can use a lambda expression as the argument to the Remove If method. The parameter type of that lambda expression is the type of collection elements.

8. 3.8 Use method references with Streams

Hello everyone. In this lesson we will go over the last item of the Generics and Collections topic and the Java Se eight programmer two exam syllabus that is using method references with streams. A method reference can be used in the place of of a lambda expression if the expression body does nothing but invoke an existing method. Here is the syntax of method references starting with a class name or a variable referencing the object that the method is invoked on. Next comes a double colon token and the final part of the syntax is the method name without any arguments during compilation. Arguments to a method reference can be inferred based on the using context. There are four kinds of method references that you can use. They are reference to a static method, reference to an instance method of an arbitrary object of a particular type, reference to an instance method of a particular object, and reference to a constructor. These method references will be covered in detail later in this lesson. Here is a class used for the demonstration of reference to a static method. This is a static method that will be called through a method reference specified in a stream operation. This method prints the full name of the person object argument it receives.

This is an example that illustrates the first kind of method references. After a stream of person is created, the full name of each stream element is printed out on the console by this lambda expression. The full name method over here is static and its argument is a stream element. Hence, this lambda expression can be rewritten using a method reference like this the method reference starts with the name of the class, followed by a double colon token, then ends with the method name. The referenced method prints the full name of each person object in the stream. Thus the output is John Doe and Jane Rowe. Here is a new version of the person class used for demonstrating reference to an instance method of an arbitrary object of a particular type.

This is an instance method that returns a boolean value and that will be invoked via a method reference in our example. This is the example showing how a reference to an instance method of an arbitrary object of a particular type can be used after a stream of person is constructed. It is filtered, allowing only person objects whose gender is male to go through. You can pass a lambda expression as the predicate argument to the filter operation. This expression just calls an instance method on a person object, so it can be replaced by a method reference, as shown here.

Similar to reference to a static method, this kind of reference begins with a class name, then a double colon token and a method name. You may have noticed that the syntax of reference to a static method is exactly the same as reference to an instance method of an arbitrary object of a particular type, so how can we distinguish them? There is a key difference in the invocation of the referenced method. If it is a static method, the current object is passed as an argument to the method. On the contrary, if the referenced method is an instance one, it is called on the current object itself. When a method reference is specified in a stream operation, the current object is a stream element.

Here is another version of the person class. Instances of this class can be sorted using a person sorter object. Note that this sort method has two parameters of the same type and returns an intvalue. This is the same as the compare method of the comparator interface. As a result, the sort method over here can be referenced when a comparator is expected. In this example, the person eva is added to the list first, then the person adam. When a sequential stream is created from the list, the element order is maintained. A person sorter instance is created here and its sort method can be used as the body of a lambda expression, which is the argument to the sorted stream operation. Stream elements are then printed on the console in another lambda expression. In both of these cases, the lambda expression body does only one thing calling an instance method of an object. Therefore, both expressions can be rewritten as method references, as shown here.

Notice that the outstatic field of the system class refers to the standard output stream, which is an instance of the print stream type. This is the declaration of the data class which will be used in our next example featuring reference to a constructor. The constructor of this class generates a random value for its internal state. The data constructor you have just seen can be used as a supplier to generate a stream. The invocation of this constructor in the lambda expression body can be replaced by a method reference, which you can see over here. The syntax of this kind of reference is similar to that of reference to a static method, except that the new keyword is used instead of a method name. Since the data constructor generates random integers, the number of even integers passing using this predicate is arbitrary. 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 log method of class data is static. Therefore, when this method is called via a method reference, the current object or a stream element in this case is passed in as an argument. However, that log method does not have any parameter. As a result, this statement fails to be compiled and the correct answer is C. Now for the second question in this question is even is an instance method when being called via a method reference in a stream operation, such a method is invoked on the object that is the stream element. Therefore, there is nothing wrong in this method reference.

The predicate represented by such a method reference filters out all data objects whose VAR property is not even so. The correct answer is B. In this lesson, we have gone over different kinds of method references and how they can be used with streams. In short, a method reference can be used in the place of a lambda expression if the expression body does nothing. But invoke an existing method, you can use four kinds of method references reference to a static method, reference to an instance method of an arbitrary object of a particular type, reference to an instance method of a particular object, and reference to a constructor.

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!