0% found this document useful (0 votes)
12 views26 pages

12 03 Functional Interface

The document discusses functional interfaces in Java. It provides motivation for using functional interfaces instead of anonymous classes. It describes the basic concepts of functional interfaces including the Predicate interface. It then demonstrates using Predicate and BiPredicate interfaces to define predicates for rectangle objects.

Uploaded by

Brain Wonderson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views26 pages

12 03 Functional Interface

The document discusses functional interfaces in Java. It provides motivation for using functional interfaces instead of anonymous classes. It describes the basic concepts of functional interfaces including the Predicate interface. It then demonstrates using Predicate and BiPredicate interfaces to define predicates for rectangle objects.

Uploaded by

Brain Wonderson
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Functional Interface

Object-Oriented Programming

Mario Simaremare, S.Kom., M.Sc.


Program Studi Sarjana Sistem Informasi
Institut Teknologi Del
Objectives

• The objectives of this session are the following:


• The students are able to describe the motivation behind
the functional interface.
• The students are able to describe the basic concepts of
functional interface.
• The students are able to use functional interface
in the solution.

Object-Oriented Programming 2
Outlines

1. Motivation.
2. Functional interface: the basic concepts.
3. Functional interface in action.

Object-Oriented Programming 3
Motivation

Object-Oriented Programming 4
Modular solution

• In the spirit of reusability, the solution should be decomposed


into smaller reusable units.
• There could be a scenario where the unit:
• is very simple and small.
• can be implemented in a class with a method.
• requires no object context.
• In this situation, we could use an anonymous class.
• As an alternative to this, we could create a “function” and use it.
• In Java, a unit in the form of function is referenced as functional interface.

Object-Oriented Programming 5
Functional Interface:
The Basic Concepts

Object-Oriented Programming 6
Functional interface

• A functional interface is an interface with exactly one


abstract (unimplemented) method.
• The abstract method should be annotated with
the @FunctionalInterface annotation.
• Java provides some basic functional interfaces.
• See the java.util.function package.

• Related concepts:
• Generics & Nested Constructs (Anonymous Class).

Object-Oriented Programming 7
The Predicate interface.

Object-Oriented Programming 8
https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

Object-Oriented Programming 9
As an example, we are going to use both
the Predicate and BiPredicate interfaces.

Both interfaces have:


• An abstract method (test()).
• And some default methods.

https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html

Object-Oriented Programming 10
The Predicate interface

• Object-oriented approach brings our solution closer


to the real world sensing.

• In the real world, every object has predicates.


• The number of predicates depends on the context and
the applied perspectives.
• E.g. There is a car, the car is an object. Possible predicates:
• The car can run fast (has top speed ≥100 kmph).
• The car is expensive (priced ≥ 1 mil. USD).
• The car is family car (has ≥ 7 seats).

Object-Oriented Programming 11
The Predicate interface

• What about a student? What predicates it may have?


• The student is a cum laude candidate (has GPA ≥ 3.51/4.00).
• The student is a veteran (at semester ≥ 9).
• Can you mention other predicates?

Object-Oriented Programming 12
Other functional interfaces have similar
characteristics to the Predicate.

However, please read the documentation.

Object-Oriented Programming 13
Functional Interface
In Action

Object-Oriented Programming 14
Case study

• Consider the following case study.


• Say, we have a rectangle object,
then here is the list of possible predicates:
• The rectangle is a square.
• When its height is equal to its width.
• The rectangle is a jumbo square.
• When it is a square and its height ≥ 20.0.

Object-Oriented Programming 15
package example.xlambda;

import java.util.function.Predicate;
Using functional interface
public class Driver {
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle(10.0, 12.0);
A simple record
Rectangle rectangle2 = new Rectangle(22.0, 22.0); to represent rectangle.
package example.xlambda;

Predicate<Rectangle> isSquare = public record Rectangle(double height, double width) {


new Predicate<Rectangle>() { public String toString() {
@Override return "h:" + this.height + "|w:" + this.width;
public boolean test(Rectangle t) { }
return t.height() == t.width(); An object of the Predicate anonymous }
}
implementation.
};

Predicate<Rectangle> isJumboSquare = Override the abstract test() method.


new Predicate<Rectangle>() {
@Override
Please read the docs for details.
public boolean test(Rectangle t) {
return t.height() == t.width() && t.height() >= 20;
}
};

System.out.println(rectangle1);
System.out.println(isSquare.test(rectangle1));
System.out.println(isJumboSquare.test(rectangle1));

System.out.println(rectangle2);
System.out.println(isSquare.test(rectangle2));
System.out.println(isJumboSquare.test(rectangle2));
}
} h:10.0|w:12.0
false
false
h:22.0|w:22.0
true
Object-Oriented Programming 16
true
package example.xlambda;

import java.util.function.Predicate;
Chaining functional interface
public class Driver { package example.xlambda;
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle(10.0, 12.0); import java.util.function.BiPredicate;
Rectangle rectangle2 = new Rectangle(22.0, 22.0); import java.util.function.Predicate;

Predicate<Rectangle> isSquare = public class Driver {


new Predicate<Rectangle>() { public static void main(String[] args) {
@Override Rectangle rectangle1 = new Rectangle(10.0, 12.0);
public boolean test(Rectangle t) { Rectangle rectangle2 = new Rectangle(22.0, 22.0);
return t.height() == t.width();
} Usingthe BiPredicate interface. Predicate<Rectangle> isSquare =
}; new Predicate<Rectangle>() {
It accepts two parameters. @Override
Predicate<Rectangle> isJumboSquare = Please see the docs for details. public boolean test(Rectangle t) {
new Predicate<Rectangle>() { return t.height() == t.width();
@Override }
public boolean test(Rectangle t) { };
return t.height() == t.width() && t.height() >= 20;
} BiPredicate<Rectangle, Predicate<Rectangle>> isJumboSquare =
}; new BiPredicate<Rectangle, Predicate<Rectangle>>() {
@Override
System.out.println(rectangle1); public boolean test(Rectangle t, Predicate<Rectangle> u) {
System.out.println(isSquare.test(rectangle1)); return u.test(t) && t.height() >= 20;
System.out.println(isJumboSquare.test(rectangle1)); }
}; Passing another object
System.out.println(rectangle2); of functional interface.
System.out.println(isSquare.test(rectangle2)); System.out.println(rectangle1);
System.out.println(isJumboSquare.test(rectangle2)); System.out.println(isSquare.test(rectangle1));
} System.out.println(isJumboSquare.test(rectangle1, isSquare));
} h:10.0|w:12.0
false System.out.println(rectangle2);
false System.out.println(isSquare.test(rectangle2));
h:22.0|w:22.0 System.out.println(isJumboSquare.test(rectangle2, isSquare));
true
Object-Oriented Programming }
17
true }
Object-Oriented Programming 18
Case study

• Say we are going to have a phone number printer.


• This printer would accept a phone number then
print out the formatted form of the phone number.

• Specs:
• A phone number can only consist of numerical characters.
• We could use a regular expression (regex) here.
• The length is, say, fixed to 10.

Object-Oriented Programming 19
package example.anonymousclass;

public class PhoneNumberPrinter {


Anonymous class version
static final String regex = "[^0-9]";
static final int NUMBER_LENGTH = 10;
package example.anonymousclass;
public void print(String phoneNumber) {
public interface IPhoneNumberFomatter {
IPhoneNumberFomatter formatter = new IPhoneNumberFomatter() {
abstract IPhoneNumberFomatter take(String phoneNumber);
private String formattedPhoneNumber;
abstract String getFormattedPhoneNumber();
public IPhoneNumberFomatter take(String phoneNumber) {
}
this.formattedPhoneNumber = phoneNumber.replaceAll(regex, "");

if (this.formattedPhoneNumber.length() != NUMBER_LENGTH) {
this.formattedPhoneNumber = null;
} package example.anonymousclass;
return this; public class Driver {
}
public static void main(String[] args) {
public String getFormattedPhoneNumber() { PhoneNumberPrinter printer =
return this.formattedPhoneNumber; new PhoneNumberPrinter();
}
printer.print("0123-456-789");
@Override printer.print("0123-456-7890");
public String toString() { }
return this.formattedPhoneNumber + }
"|" + phoneNumber;
}
};

System.out.println(formatter.take(phoneNumber));
}
} 0123456789|0123-456-789
null|0123-456-7890

Object-Oriented Programming 20
Anonymous class version
package example.anonymousclass;
Is there any representative interface in
the java.util.function package for this? public interface IPhoneNumberFomatter {
abstract IPhoneNumberFomatter take(String phoneNumber);

abstract String getFormattedPhoneNumber();


}

package example.anonymousclass;

public class Driver {

public static void main(String[] args) {


PhoneNumberPrinter printer =
new PhoneNumberPrinter();

printer.print("0123-456-789");
printer.print("0123-456-7890");
}
}

0123456789|0123-456-789
null|0123-456-7890

Object-Oriented Programming 21
We could use the Consumer interface.
It accepts a parameter without providing any return.

Alternatively, we could also use the Function interface.


It also accepts a parameter but provides a return.

Object-Oriented Programming 22
Functional interface version
package example.anonymousclass;

import java.util.function.Consumer;

public class Driver2 {

public static void main(String[] args) {


Consumer<String> printer = new Consumer<String>() {
@Override
public void accept(String t) {
Using the Consumer interface. final String regex = "[^0-9]";
final int NUMBER_LENGTH = 10;

String formattedPhoneNumber = t.replaceAll(regex, "");

if (formattedPhoneNumber.length() != NUMBER_LENGTH) {
formattedPhoneNumber = null;
}

System.out.println(formattedPhoneNumber + "|" + t);


}
};

printer.accept("0123-456-789");
printer.accept("0123-456-7890");
}
}

0123456789|0123-456-789
null|0123-456-7890

Object-Oriented Programming 23
Todo

• Explore some other functional interface


available at java.util.function package.
• Try to implement the java.util.Arrays’ sort() method.

• Try to identify and apply the concept into your project.

Object-Oriented Programming 24
References

• Cay Horstman. Core Java.


• Matt Weisfeld. The Object-Oriented Thought Process.

Object-Oriented Programming 25
Thank
you

Object-Oriented Programming 26

You might also like