Table of Contents
Overview
This blog post will focus on a specific Java 8 feature, providing practical examples to help you understand how to leverage these features effectively in your own projects. We will cover practical code example regarding Lambda Expression, Predicate , Consumer, Function interface etc.
Example 1: Check User Name & Password Using Predicate<T>
import java.util.function.Predicate;
public class PredicateUserAuth {
public static void main(String[] args) {
// Create Predicate to check the credential
Predicate predicate=u-> u.getUname().equals("sachin") && u.getUpass().equals("123");
// Check if user is valid or not
boolean isValid=predicate.test(new User("sachin","123"));
if (isValid){
System.out.println("User Authentication Success");
}else{
System.out.println("Authentication Failed");
}
}
}
class User{
private String uname;
private String upass;
public User(String uname, String upass) {
this.uname = uname;
this.upass = upass;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpass() {
return upass;
}
public void setUpass(String upass) {
this.upass = upass;
}
}
Example 2: Filtering Employee Details by Designation, Salary, and Designation & salary both by joining two Predicates
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class EPM {
public static void main(String[] args) {
List empList=new ArrayList<>();
addEmp(empList);
// System.out.println(empList);
// Predicate to Check designation for manager
Predicate manager_predicate=emp->emp.getDesignation().equals("Manager");
// Predicate to get the list of employee for whome salary is >70000
Predicate salary_predicate=emp->emp.getSalary()>75000;
// (Joining two Predicate)Get the list of employee whose salary is >70000 & designation is manager
Predicate salary_desig_predicate=salary_predicate.and(manager_predicate);
System.out.println("List of Employees salary >75000");
getSalaryMoreThan70(salary_predicate,empList);
System.out.println("-----------------------------");
System.out.println("List of Employees whom designation is Manager");
getDesignation(manager_predicate,empList);
System.out.println("-----------------------------");
System.out.println("List of Employees who are Manager & Salary > 70000");
getSalaryWithDesignation(salary_desig_predicate,empList);
}
private static void getSalaryWithDesignation(Predicate salaryDesigPredicate, List list) {
for (Employee e:list){
if (salaryDesigPredicate.test(e)){
System.out.println(e);
}
}
}
private static void getSalaryMoreThan70(Predicate salaryPredicate, List list) {
for (Employee e:list){
if (salaryPredicate.test(e)){
System.out.println(e);
}
}
}
private static void getDesignation(Predicate managerPredicate,List list) {
for (Employee e:list){
if (managerPredicate.test(e)){
System.out.println(e);
}
}
}
public static void addEmp(List list){
list.add(new Employee("sachin","SDE",50000.0,"HYD"));
System.out.println();
list.add(new Employee("Devid", "Manager",70000.0,"New Jersey"));
list.add(new Employee("Alex", "Manager",100000.0,"Delhi"));
list.add(new Employee("Jay", "SDE",78000.0,"Noida"));
list.add(new Employee("Daniel", "Manager",95000.0,"NCR"));
list.add(new Employee("Vimal", "Developer",54000.0,"Adelaide"));
list.add(new Employee("Neha", "Manager",65000.0,"Adelaide"));
}
}
class Employee{
private String name;
private String designation;
private Double salary;
private String city;
public Employee(String name, String designation, Double salary, String city) {
this.name = name;
this.designation = designation;
this.salary = salary;
this.city = city;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
// String s=String.format("(%s,%s,%.2f,%s),name,designation,salary,city)");
String s = String.format("(%s,%s,%.2f,%s)",name,designation,salary,city);
return s;
}
}
Output:
List of Employees salary >75000
(Alex,Manager,100000.00,Delhi)
(Jay,SDE,78000.00,Noida)
(Daniel,Manager,95000.00,NCR)
-----------------------------
List of Employees whom designation is Manager
(Devid,Manager,70000.00,New Jersey)
(Alex,Manager,100000.00,Delhi)
(Daniel,Manager,95000.00,NCR)
(Neha,Manager,65000.00,Adelaide)
-----------------------------
List of Employees who are Manager & Salary > 70000
(Alex,Manager,100000.00,Delhi)
(Daniel,Manager,95000.00,NCR)
Related Topics