Found 4348 Articles for Java 8

How to iterate the contents of a collection using forEach() in Java?

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

250 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

How to use UnaryOperator interface in lambda expression in Java?

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

800 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

How to use BinaryOperator interface in lambda expression in Java?

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

552 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

How to use Supplier interface in lambda expression in Java?

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

5K+ 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

How to use Predicate and BiPredicate in lambda expression in Java?

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

1K+ 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

How to sort a list by name using a Comparator interface in Java?

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

578 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

How to use Function and BiFunction interfaces in lambda expression in Java?

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

1K+ Views

The Function interface is a pre-defined functional interface that can be used as an assignment target for a lambda expression or method reference. It takes a single parameter and returns result by calling the apply() method. While the BiFunction interface is also a pre-defined functional interface that takes two parameters and returns a result. It is similar to the Function interface except it takes two parameters.Syntax@FunctionalInterface public interface Function @FunctionalInterface public interface BiFunctionExampleimport java.util.function.BiFunction; import java.util.function.Function; public class SampleFunctionBiFunctionTest {    public static void main(String[] args) {       Function printNumber = a -> a*10;       System.out.println("The number is: "+ printNumber.apply(10));   ... Read More

How to use Consumer and BiConsumer interfaces in lambda expression in Java?

raja
Updated on 13-Jul-2020 06:30:59

2K+ Views

A Consumer interface is a predefined functional interface that can be used when creating lambda expressions or method references. This interface represents an operation that accepts a single input parameter and doesn't return anything. It contains only one method named accept(). The BiConsumer interface is similar to a Consumer interface and accepts two input parameters and doesn’t return anything.Syntax@FunctionalInterface public interface Consumer @FunctionalInterface public interface BiConsumerExampleimport java.util.*; import java.util.function.*; public class ConsumerBiConsumerTest {    public static void main(String[] args) {       Consumer c = (x) -> System.out.println(x.toLowerCase());  // lambda expression       c.accept("Raja");         Consumer con = (x) -> {  // ... Read More

How to implement an instance method reference using a class name in Java?

raja
Updated on 13-Jul-2020 05:40:25

614 Views

Method reference is a simplified form of the lambda expression. It can specify a class name or instance name followed by the method name. The "::" symbol can separate a method name from the name of an object or class.An instance method reference refers to an instance method of any class. In the below example, we can implement an instance methods reference using the class name.Syntax::Exampleimport java.util.*;; import java.util.function.*; public class ClassNameRefInstanceMethodTest {    public static void main(String args[]) {       List empList = Arrays.asList(          new Employee("Raja", 15000),          new Employee("Adithya", 12000),          new Employee("Jai", 9000),   ... Read More

How can we write Callable as a lambda expression in Java?

raja
Updated on 13-Jul-2020 05:32:03

5K+ Views

A Callable interface defined in java.util.concurrent package. An object of Callable returns a computed result done by a thread in contrast to a Runnable interface that can only run the thread. The Callable object returns Future object that provides methods to monitor the progress of a task executed by a thread. An object of the Future used to check the status of a Callable interface and retrieves the result from Callable once the thread has done.In the below example, we can write a Callable interface as a Lambda Expression.Exampleimport java.util.concurrent.*; public class LambdaCallableTest {    public static void main(String args[]) throws InterruptedException {       ExecutorService executor = Executors.newSingleThreadExecutor();   ... Read More

Advertisements