Use BooleanSupplier in Lambda Expression in Java

raja
Updated on 13-Jul-2020 08:34:39

3K+ Views

BooleanSupplier is a functional interface defined in the "java.util.function" package. This interface can be used as an assignment target for a lambda expression or method reference. BooleanSupplier interface has only one method getAsBoolean() and returns a boolean result, true or false.Syntax@FunctionalInterface public interface BooleanSupplier {    boolean getBoolean(); }Exampleimport java.util.function.BooleanSupplier; public class BooleanSupplierLambdaTest {    public static void main(String[] args) {       BooleanSupplier Obj1 = () -> true;       BooleanSupplier Obj2 = () -> 5 < 50; // lambda expression       BooleanSupplier Obj3 = () -> "tutorialspoint.com".equals("tutorix.com");       System.out.println("Result of Obj1: " + Obj1.getAsBoolean());       ... Read More

Use FileFilter Interface in Lambda Expression in Java

raja
Updated on 13-Jul-2020 08:30:13

828 Views

A FileFilter is a functional interface from the "java.io" package. It can be used as the assignment target for a lambda expression or method reference. An instance of the FileFilter interface passed to the listFiles() method of the File class. FileFilter interface having one abstract method accept() and it tests whether or not the specified abstract pathname has included in a pathname list.Syntax@FunctionalInterface public interface FileFilterExampleimport java.io.File; import java.io.FileFilter; public class FileFilterTest {    public static void main(String[] args) {       File dir = new File("C:/Program Files/Java/jdk1.8.0_211");       File[] subDir = dir.listFiles((file) -> {    // lambda expression         ... Read More

Implement PropertyChangeListener Using Lambda Expression in Java

raja
Updated on 13-Jul-2020 08:13:49

5K+ Views

A PropertyChangeListener is a functional interface from java.beans package. It has one abstract method propertyChange() and gets called when a bound property is changed. This method takes a PropertyChangeEvent argument that has details about an event source and the property that has changed. A PropertyChangeSupport can be used by beans that support bound properties. It can manage a list of listeners and dispatches property change events. The PropertyChangeListener's implementer does the role of an Observer and bean wrapping the PropertyChangeSupport is Observable.Syntaxvoid propertyChange(PropertyChangeEvent evt)Exampleimport java.beans.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class PropertyChangeListenerLambdaTest {    public static void main(String [] args) {   ... Read More

Iterate Contents of a Collection Using foreach in Java

raja
Updated on 13-Jul-2020 08:06:06

332 Views

Lambda expression is the anonymous representation of a function descriptor of a functional interface. As we know that all collection interfaces like List, Set and Queue use an Iterable as their super interface. Since Java 8, an Iterable interface introduces a new method called forEach(). This method performs an action on the contents of Iterable in the order elements that occur when iterating until all elements have processed.Syntaxvoid forEach(Consumer

Use UnaryOperator Interface in Lambda Expression in Java

raja
Updated on 13-Jul-2020 08:00:30

1K+ Views

UnaryOperator is a functional interface that extends the Function interface. It represents an operation that accepts a parameter and returns a result of the same type as its input parameter. The apply() method from Function interface and default methods: andThen() and compose() are inherited from the UnaryOperator interface. A lambda expression and method reference can use UnaryOperator objects as their target.Syntax@FunctionalInterface public interface UnaryOperator extends FunctionExample-1import java.util.function.UnaryOperator; public class UnaryOperatorTest1 {    public static void main(String[] args) {       UnaryOperator operator = t -> t * 2;   // lambda expression       System.out.println(operator.apply(5));       System.out.println(operator.apply(7));       System.out.println(operator.apply(12));    } }Output10 ... Read More

Use BinaryOperator Interface in Lambda Expression in Java

raja
Updated on 13-Jul-2020 07:29:22

1K+ Views

BinaryOperator is one of a functional interface from java.util.function package and having exactly one abstract method. A lambda expression or method reference uses BinaryOperator objects as their target. BinaryOperator interface represents a function that takes one argument of type T and returns a value of the same type.BinaryOperator Interface contains two static methods, minBy() and maxBy(). The minBy() method returns a BinaryOperator that returns the greater of two elements according to a specified Comparator while the maxBy() method returns a BinaryOperator that returns the lesser of two elements according to a specified Comparator.Syntax@FunctionalInterface public interface BinaryOperator extends BiFunctionExampleimport java.util.function.BinaryOperator; public class BinaryOperatorTest {    public static void main(String[] args) {       BinaryOperator ... Read More

Use Supplier Interface in Lambda Expression in Java

raja
Updated on 13-Jul-2020 07:22:11

7K+ Views

A Supplier interface is a pre-defined interface that represents the supplier of results. It is instantiated using lambda expression or method reference or default constructor. The functional method of a Supplier interface is the get() method. This interface belongs to java.util.function package.Syntax@FunctionalInterface public interface SupplierIn the below program, we can use the Supplier interface in a lambda expression. The get() method only returns a value and doesn't accept any argument, so a lambda expression having an empty argument part.Exampleimport java.util.*; import java.util.function.*; public class SupplierTest {    public static void main(String args[]) {       Supplier supplier1 = () -> "TutorialsPoint";   // lambda expression       System.out.println(supplier1.get());       ... Read More

Use Predicate and BiPredicate in Lambda Expression in Java

raja
Updated on 13-Jul-2020 07:18:22

2K+ Views

A Predicate interface defined in java.util.function package. It represents a boolean-valued function with one argument. It is kind of a functional interface whose functional method is the test(). BiPredicate interface is similar to the Predicate interface with two arguments. It can be used as an assignment target for a lambda expression.Syntax for Predicate@FunctionalInterface public interface PredicateExampleimport java.util.*; import java.util.function.*; import java.util.stream.*; public class EmployeePredicateTest {    public static void main(String[] args) {       Employee e1 = new Employee(1, 23, "M", "Raja");       Employee e2 = new Employee(2, 13, "M", "Jai");       Employee e3 = new Employee(3, 36, "F", "Yamini");       ... Read More

Symmetric Min-Max Heaps

Arnab Chakraborty
Updated on 13-Jul-2020 07:15:03

2K+ Views

A symmetric min-max heap (SMMH) is defined as a complete binary tree in which each node except the root has exactly one element. The root of an SMMH be empty and the total number of nodes in the SMMH is m + 1, where m is the number of elements.Let y be any node of the SMMH. Let elements(y) be the elements in the sub tree rooted at y but excluding the element (if any) in y. Assume that elements(y) j= ∅. y satisfies the following properties:The left child of y has the minimum element in elements(y).The right child of ... Read More

Sort a List by Name Using Comparator Interface in Java

raja
Updated on 13-Jul-2020 07:00:37

837 Views

Comparator interface can be used to order the objects of user-defined classes. It is capable of comparing two objects of two different classes. We can sort a list of objects where we can't modify the object’s source code to implement Comparable interface. A lambda expression can't be executed on its own and used to implement methods that are declared in a functional interface.In the below example, we need to sort a list by name using the Comparator interface and Stream API with the help of lambda expressions.Exampleimport java.util.*; import java.util.function.*; import java.util.stream.*; public class ListSortByNameTest {    public static void main(String[] args) {       List arrayList = new ArrayList(); ... Read More

Advertisements