Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Java 8 - Functional Interfaces

This document discusses Java 8 functional interfaces and provides an example. It defines a functional interface as having a single method to implement. It lists the Predicate interface as an example that tests an object and returns a boolean. The document then provides a Java program example that uses the Predicate interface to filter a list of integers based on different criteria and print the results.

Uploaded by

Nahom Dires
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Java 8 - Functional Interfaces

This document discusses Java 8 functional interfaces and provides an example. It defines a functional interface as having a single method to implement. It lists the Predicate interface as an example that tests an object and returns a boolean. The document then provides a Java program example that uses the Predicate interface to filter a list of integers based on different criteria and print the results.

Uploaded by

Nahom Dires
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/10/22, 8:36 PM Java 8 - Functional Interfaces

Java 8 - Functional Interfaces

Functional interfaces have a single functionality to exhibit. For example, a Comparable interface
with a single method ‘compareTo’ is used for comparison purpose. Java 8 has defined a lot of
functional interfaces to be used extensively in lambda expressions. Following is the list of
functional interfaces defined in java.util.Function package.

Given below is the list of interfaces in Java8.

Functional Interface Example


Predicate <T> interface is a functional interface with a method test(Object) to return a Boolean
value. This interface signifies that an object is tested to be true or false.

Create the following Java program using any editor of your choice in, say, C:\> JAVA.

Java8Tester.java
 Live Demo
import java.util.Arrays;

import java.util.List;

import java.util.function.Predicate;

public class Java8Tester {

public static void main(String args[]) {

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);

// Predicate<Integer> predicate = n -> true

// n is passed as parameter to test method of Predicate interface

// test method will always return true no matter what value n has.

System.out.println("Print all numbers:");

//pass n as parameter

eval(list, n->true);

// Predicate<Integer> predicate1 = n -> n%2 == 0

// n is passed as parameter to test method of Predicate interface

// test method will return true if n%2 comes to be zero

https://www.tutorialspoint.com/java8/java8_functional_interfaces.htm 1/3
8/10/22, 8:36 PM Java 8 - Functional Interfaces

System.out.println("Print even numbers:");

eval(list, n-> n%2 == 0 );

// Predicate<Integer> predicate2 = n -> n > 3

// n is passed as parameter to test method of Predicate interface

// test method will return true if n is greater than 3.

System.out.println("Print numbers greater than 3:");

eval(list, n-> n > 3 );

public static void eval(List<Integer> list, Predicate<Integer> predicate

for(Integer n: list) {

if(predicate.test(n)) {

System.out.println(n + " ");

Here we've passed Predicate interface, which takes a single input and returns Boolean.

Verify the Result


Compile the class using javac compiler as follows −

C:\JAVA>javac Java8Tester.java

Now run the Java8Tester as follows −

C:\JAVA>java Java8Tester

It should produce the following output −

Print all numbers:

https://www.tutorialspoint.com/java8/java8_functional_interfaces.htm 2/3
8/10/22, 8:36 PM Java 8 - Functional Interfaces

Print even numbers:


2

Print numbers greater than 3:

https://www.tutorialspoint.com/java8/java8_functional_interfaces.htm 3/3

You might also like