
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between Function and Predicate in Java 8
Function and Predicate both functional interface was introduced in Java 8 to implement functional programming in Java.
Function interface is used to do the transformation.It can accepts one argument and produces a result. On the other side, Predicate can also accept only one argument but it can only return boolean value. It is used to test the condition.
Sr. No. | Key | Function | Predicate |
---|---|---|---|
1 | Basic | It can take 2 type parameters First one represents input type argument type and second one represents return type. | It can take one type parameter which represents input type or argument type. |
2 | Return Type | It can return any type of value. | It can only return boolean value |
3 | Method | It has abstract method apply(). | It has abstract method test(). |
4. | Use Case | It can be used to implement conditional checks | It can be used for the transformation and to the return values. |
Example of Predicate
public class Main { public static void main(String args[]) { List<Integer> numList = new ArrayList<>(); numList.add(5); numList.add(10); Predicate<Integer> pred = i -> i > 5; numList.stream().filter(pred).forEach(i -> System.out.println(i)); } }
Example of Function
public class Main { public static void main(String args[]) { List<Integer> numList = new ArrayList<>(); numList.add(78); numList.add(10); Function<Integer, Integer> fun = i -> i / 2; numList.stream().map(fun).forEach(System.out::println); } }
- Related Articles
- Difference between Streams and Collections in Java 8
- Difference between intermediate and terminal operations in Java 8
- Difference between default and static interface method in Java 8.
- How to negate a predicate function in JavaScript?
- Difference between Function and Procedure
- Difference between constructor and ngOnInit in Angular 8
- Difference between Method and Function in C#
- Difference between Method and Function in Python
- Differences between Java 8 and Java 9?
- How to use Predicate and BiPredicate in lambda expression in Java?
- Difference between SCALAR and COLUMN function
- Difference Between Function Overloading and Overriding in C++
- Difference between gettype() in PHP and get_debug_type() in PHP 8
- Importance of Predicate interface in lambda expression in Java?
- Difference Between Friend Function and Friend Class

Advertisements