1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Lambda Expressions and Static Imports

Predicate

Contents

keyboard_tab
Lambda Expressions and Static Imports
1
Course Overview
PREVIEW27s
4
Predicate
9m 55s

The course is part of this learning path

Start course
Overview
Difficulty
Beginner
Duration
35m
Students
40
Ratings
5/5
starstarstarstarstar
Description

In this lesson, we will learn the Lambda Expressions.

Learning Objectives

  • Marker and Functional Interfaces
  • Lambda Expressions
  • Predicate Interface
  • Static Import

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In our previous lesson, we developed a small application using the Lambda expression. In this lesson, we will use the Predicate Interface, which is a well known interface used with the Lambda expression. The Predicate interface is a functional interface defined in the java.util.function package. It improves the manageability of code and helps in unit testing them separately. Although it contains different default methods, it has only one abstract method. This is a test method that returns a Boolean value. Notice that test can take any type of object as parameter. It returns true if the condition is passed to it is met and false if not. Now, let's look at the use of the Predicate interface in a simple way. 

As you can see on the slide, we first create an instance of the Predicate class. In our example, we set the type of Predicate as integer. But as I said before, it can be any type of object. Then, we create an if condition using the Lambda symbol. If the value assigned to the i parameter is greater than 20, it returns true, otherwise false. Then, using the mPredicate reference, we call the test method defined in the Predicate class and give the parameter we want to compare. In our example, when we pass the value 30 to the test method, it prints true to the screen, because 30 is greater than 20. Yes, on the slide, you see the simplest use of the Predicate interface. Now let's move on to Eclipse and get some practice. I'll create a new class. I right click on the 'lambdaexpressionexample' package and select the new class options.

The class name can be PredicateExample. And I will check the check box for the main method and click the 'Finish' button. Okay, let's create an instance of the Predicate interface; 'Predicate< integer> predicate 1 ='. After the equal sign, I'll write the lambda expression 't ->'. Now, I'll write the condition (t < 20). Now, I'll call the test method of the Predicate interface, predicate1.test, and I'll give the 15 as the parameter. And let's assign the result to a variable, Boolean result. And let's print the result on the console, system.out.println(result). Since the 15 is less than the 20, the result will be true. Let's run and see. As you can see the result is true. This is the simplest usage of the Predicate interface. 

Also, we can chain the two predicates to each other with "AND" or "OR" operators. I'll create another Predicate instance, Predicate< integer > predicate2 = t-> (t>10). And I'll copy these lines and paste them here. And I'll convert these lines to the comment line. Instead of this code, I will Predicate1.and(Predicate2), and lastly, .test15. Since the 15 is greater than 10 and less than the 20, the result will be true. Let's run and see. As you can see the result is true. This time, let's use the OR operator. I'll copy this line and paste it here, and I will convert this line to the comment line. This will be OR operator. Since the result of the first condition is true, the result will be true again. Let's run it again. As you can see the result is true again.

Okay, now let's use the Predicate interface with a method. Let's make a small application like this. Let's enter a name from the console screen. If the name is what we expect, we'll print Welcome, with the name entered on the console. If not, we'll print the unknown person message. Let's do this using the Predicate interface. First, I'll convert these lines to the comment line. Outside the main method, I'll create a public static void method. The name of this method can be showResult and this method will take two parameters. The first parameter is the string name and the second parameter is Predicate < string > mPredicate. Inside the method, I will create an if-else statement. if mPredicate.test(name). So, I will check the name. If the condition is true, this time I will show a message s.out ("Welcome" + name), else s.out("Unknown person"). Okay, now let's call this method inside the main method, showResult, and I need to give the parameters of this method. For this, first I will import the scanner class; Scanner inputScanner = new Scanner(System.in), and I'll give a message to the user, s.out("Please verify your name: "). 

Now I will take the name and assign it a variable. String userName = inputScanner.next(). Okay, now we can pass the username variable as the parameter to show the result method. And as for the second parameter, we will create the condition with the Lambda expression, personName; you can write anything here, -> in parentheses, I write personName.equals David. So, if we enter the David into the console, the condition will be true and the welcome David message will be printed on the console. Otherwise, the unknown person message will be printed. And let's close the input scanner object. Let's run and see. I will enter Alex. The condition was not met and the unknown person message was printed to the console. Let's run it one more time. This time, I'm entering David. This time the condition is met and the message "Welcome David" is printed on the console. Okay, finally let's use the Predicate interface with the array list. I'll convert these lines to the comment line. I'll create an array list, ArrayList< string > cities = new ArrayList<>.

Let's add some elements to this list, cities.add("London"). I'll copy and paste this line four times. This can be Paris, this can be New York, this can be Berlin, and this can be Tokyo. First, I will print the elements of this list on the console; s.out("cities: " + cities). Now, I will remove New York from the list using the Predicate, cities.removeIf. If you notice, the parameter of the removeIf method is the Predicate interface. So we can create a Lambda expression here. city -> city.equals New York. As you can see we can filter easily thanks to the Predicate Interface. If there's an element like New York in the list, it will be deleted from the list. Otherwise, the list will stay the same. Finally, let's print the elements of the list to the console once again; s.out ("after predicate: " + cities). Let's run and see. As you can see, New York has been deleted from the list. Yes, I think you understand the usage of the Predicate interface. The thing to keep in mind is that the Predicate interface is a functional interface. And there is an abstract method called test that returns a Boolean value. Thanks to this method, we can perform simple filtering and condition statements. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
458
Courses
47
Learning Paths
3

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics