Showing posts with the label Consumer

Learn Java Predicate, Consumer, Function, and Supplier (Functional Interfaces) with Example

Java is Object Oriented Programming language, being object-oriented is not bad, but it brings a lot of verbosity to the program.Java 8 introduced new libraries and programming styles, for example, functional interfaces, lambda expressions, and streams. These bring functional-style programming to the object-oriented programming capabilities of Java. Java Functional Interface and Lambda Expression help us write smaller and cleaner code by removing much boilerplate code.  A functional interface contains only a single abstract method and  can include default and static methods that do have an implementation and a single unimplemented method. Here we will discuss Predicate, Consumer, Function, and Supplier. 1. Predicate Interface The Predicate is a functional interface defined in java. util. Function package , which accepts an argument and returns a boolean. This is mainly used to filter data from a Java Stream. The filter method of a stream accepts a predicate to filter the data and retur