Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


Lambda Supplier, Consumer and Predicate


Supplier is an interface in java.util.function package that represent supplier of results. Supplier can be used in all contexts where there is no input but an output is expected. Supplier can be assigned in lambda expression. Supplier can be passed as an argument to different methods in java 8. Supplier has a functional method get().


LambdaSupplier.java

package java8;

import java.util.function.Supplier;

public class LambdaSupplier
{
public static void main(String[] args) {
Supplier<String> supplier = () -> "Test User"; System.out.println(supplier.get()); }
}
 


Consumer. java.util.function.Consumer is a functional interface whose functional method is accept (T t). The Consumer interface represents an operation that takes single argument T and returns no result. We can use a consumer to call println or other void methods. It is opposite to supplier.


LambdaConsumer.java

package java8;

import java.util.function.Consumer;

public class LambdaConsumer
{
public static void main(String[] args) {
Consumer<String> consumerStr = s -> { System.out.println(s); }; consumerStr.accept("Welcome to javalogin.");

Consumer<Integer> consumerInt=i-> {
System.out.println("Integer value="+i);
};
consumerInt.accept(5);

Consumer<String> consumer = Test :: printNames;
consumer.accept("vaibhav");
}

private static void printNames(String name) {
System.out.println(name);
}

}
 


Predicate is a new functional interface defined in java.util.function package which can be used in all the contexts where an object needs to be evaluated for a given test condition and a boolean value needs to be returned based on whether the condition was successfully met or not. Predicate interface contains abstract method test that takes a generic argument and returns a boolean. It tests whether the T argument satisfies a condition.

LambdaPredicate.java

package java8;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class LambdaPredicate
{
public static void main(String[] args) {
List<String> employees = new ArrayList<String>();
employees.add("Smith");
employees.add("Michael");
employees.add("Andrew");
print(employees, emp -> emp.contains("e"));

}

private static void print(List<String> employees,
Predicate<String> empPredicate) {
for (String emp : employees) {
if (empPredicate.test(emp)) {
System.out.println(emp);
}
}

}
 


Predicate<T> take input of type T and returns a Boolean result (boolean test(T t)). Essentially used as decision for some type T. filter() method take Predicate as input to filter out the collection using a test provided in Predicate.


     << Previous
Next >>