Optional is class java.util.Optional<T> introduce in java 1.8 version . Optional class is container class to represent the existence or absence of value .
These are some basic method of Optional class
isPresent :- To check existence or absence of value in Optional class .
get :- If object present in Optional class container then get method object from Optional class container otherwise throws a java.util.NoSuchElementException.
orElse:- If object present in Optional class container then get method object from Optional class container otherwise throws an Exception So to avoid this exception you can return another default object in case of exception.
Note :- Optional class also have primitive type representation of Optional class like
1.OptionalInt
2.OptionalLong
3. OptionalDouble
Optional Class Java Application
OptionalApplication.java
package in.jk.java8.stream;
import java.util.List;
import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import in.jk.java8.Employee;
public class OptionalApplication {
public static void main(String[] args) {
System.out.println("Use Of Optional class Application .....\n");
Optional<Employee> employeeContainer = null;
List<Employee> employees = Employee.getEmployeeList();
employeeContainer = employees.stream()
.filter(employee -> employee.getSalary() > 2000)
.findFirst();
if (employeeContainer.isPresent()) {
Employee employee2 = employeeContainer.get();
System.out.println("Empoyee by Get :: " + employee2);
}
employeeContainer = employees.stream()
.filter(employee -> employee.getSalary() > 20000)
.findFirst();
try {
Employee employee = employeeContainer.get();
System.out.println("Employee by GET :: " + employee);
} catch (Exception e) {
System.out.println("\nIf Employee No Prsesent in Optional Class Container :: ");
System.out.println("Error :: " + e);
}
employeeContainer = employees.stream()
.filter(employee -> employee.getSalary() > 2000)
.findFirst();
Employee masterEmployee = new Employee();
masterEmployee.setEmpId(1);
masterEmployee.setName("John");
masterEmployee.setSalary(1);
masterEmployee.setCompany("Google");
employeeContainer.orElse(masterEmployee);
Employee employee = employeeContainer.orElse(masterEmployee);
System.out.println("\n Employee by orElse if Employee is present in Optinal :: " + employee);
employeeContainer = employees.stream()
.filter(emp -> emp.getSalary() > 20000)
.findFirst();
Employee employee1 = employeeContainer.orElse(masterEmployee);
System.out.println("Employee by orElse if Employee is not present in Optinal :: " + employee1);
System.out.println("\n----Use of primitive type representation of Optional---------\n");
OptionalInt optionalInt = employees.stream()
.filter(emp -> emp.getSalary() > 2000)
.mapToInt(Employee::getSalary)
.max();
System.out.println("Max Salary by OptionalInt :: "+optionalInt);
OptionalLong optionalLong = employees.stream()
.filter(emp -> emp.getSalary() > 2000)
.mapToLong(Employee::getSalary)
.max();
System.out.println("Max Salary by OptionalLong :: "+optionalLong);
OptionalDouble optionalDouble = employees.stream()
.filter(emp -> emp.getSalary() > 2000)
.mapToDouble(Employee::getSalary)
.max();
System.out.println("Max Salary by OptionalDouble :: "+optionalDouble);
}
}
Employee.java
package in.jk.java8;
import java.util.ArrayList;
import java.util.List;
public class Employee {
private int empId;
private String name;
private String company;
private int salary;
//Getters and Setters
@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + ", company=" + company + ", salary=" + salary + "]";
}
public static List<Employee>getEmployeeList() {
List<Employee> empList =new ArrayList<Employee>();
Employee employee = new Employee();
employee.setEmpId(1);
employee.setName("J K");
employee.setCompany("Google");
employee.setSalary(1000);
Employee employee1 = new Employee();
employee1.setEmpId(2);
employee1.setName("J K");
employee1.setCompany("Google");
employee1.setSalary(2000);
Employee employee2 = new Employee();
employee2.setEmpId(3);
employee2.setName("J K");
employee2.setCompany("Google");
employee2.setSalary(3000);
Employee employee3 = new Employee();
employee3.setEmpId(4);
employee3.setName("SHA");
employee3.setCompany("APAR");
employee3.setSalary(4000);
Employee employee4 = new Employee();
employee4.setEmpId(5);
employee4.setName("HM");
employee4.setCompany("PAYTM");
employee4.setSalary(5000);
Employee employee5 = new Employee();
employee5.setEmpId(5);
employee5.setName("HM");
employee5.setCompany("APAR");
employee5.setSalary(6000);
Employee employee6 = new Employee();
employee6.setEmpId(6);
employee6.setName("JK");
employee6.setCompany("APAR");
employee6.setSalary(7000);
Employee employee7 = new Employee();
employee7.setEmpId(7);
employee7.setName("JK");
employee7.setCompany("JET BRAINS");
employee7.setSalary(8000);
Employee employee8 = new Employee();
employee8.setEmpId(8);
employee8.setName("AJ");
employee8.setCompany("APAR");
employee8.setSalary(9000);
Employee employee9 = new Employee();
employee9.setEmpId(8);
employee9.setName("AJ");
employee9.setCompany("ADOBE");
employee9.setSalary(10000);
empList.add(employee);
empList.add(employee1);
empList.add(employee2);
empList.add(employee3);
empList.add(employee4);
empList.add(employee5);
empList.add(employee6);
empList.add(employee7);
empList.add(employee8);
empList.add(employee9);
return empList;
}
}
Output in console ....
Use Of Optional class Application .....
Empoyee by Get :: Employee [empId=3, name=J K, company=Google, salary=3000]
If Employee No Prsesent in Optional Class Container ::
Error :: java.util.NoSuchElementException: No value present
Employee by orElse if Employee is present in Optinal :: Employee [empId=3, name=J K, company=Google, salary=3000]
Employee by orElse if Employee is not present in Optinal :: Employee [empId=1, name=John, company=Google, salary=1]
----Use of primitive type representation of Optional---------
Max Salary by OptionalInt :: OptionalInt[10000]
Max Salary by OptionalLong :: OptionalLong[10000]
Max Salary by OptionalDouble :: OptionalDouble[10000.0]
No comments:
Post a Comment