Java 8 built in Functional Interfaces

What is Functional programming:

Murali krishna Konduru
2 min readJan 23, 2021

Functional programming is a mathematical style of programming which executes sequentially, it takes inputs and gives output, it is not preferable to modify the input data. It’s totally about programming with values.

Sample:

function addNumbers(a, b){
return a+b;
}

Java 8 functional programming:

An interface with a single method is known as a Functional Interface.

Whether the interface have only one abstract (unimplemented) method?

Whether the parameters of the lambda expression match the parameters of the single method?

Whether the return type of the lambda expression match the return type of the single method?

If the answer is true for above three questions, then the given lambda expression is matched successfully against the interface.

Types of Functional interfaces:

These features are functional interfaces (an interface with only one abstract method) which belongs to the java.util.function package.

Consumers

@FunctionalInterface
public interface Consumer<T>

Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.This is a functional interface whose functional method is accept(Object)

package com.techpredators.functionalinterfaces;import java.util.function.Consumer;
public class ConsumerInterfaceTest
{
public static void main(String[] args) {
// Referring method to the string type of Consumer Interface.
Consumer<String> consumer = ConsumerInterfaceTest::printStrings;// No Explicit return value.consumer.accept(“Java”);
consumer.accept(“Dot Net”);
consumer.accept(“Scala”);
}
private static void printStrings(String languageName)
{
System.out.println(languageName);
}
}
@FunctionalInterface
public interface Supplier<T>

This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

package com.techpredators.functionalinterfaces;import java.util.Date;
import java.util.function.Supplier;
public class SupplierInterfaceTest {
public static void main(String args[]) {
Supplier<String> stringSupplier = () -> new String(“I am String”);System.out.println(“String in stringSupplier is ::” + stringSupplier.get() + “::”);
// Constructor
Supplier<String> stringEmpty = String::new;
System.out.println(“EmptyStr is::” + stringEmpty.get() + “::”);
// Custom method
Supplier<Date> date = SupplierInterfaceTest::returnSystemDate;
Date systemDate = date.get();
System.out.println(“systemDate::” + systemDate);
}
public static Date returnSystemDate() {
return new Date();
}
}

Predicates

@FunctionalInterface
public interface Predicate<T>

Represents a predicate (boolean-valued function) of one argument.This is a functional interface whose functional method is test(Object).

package com.techpredators.functionalinterfaces;import java.util.function.Predicate;
public class PredicateInterfaceTest {
public static void main(String[] args) {
Predicate<Integer> lesserthan = i -> (i < 18);
System.out.println(lesserthan.test(10));
}
}

--

--