Selenium - Optional in Java 8
- When Optional is used there will be no need for “Null Checks” and “NullPointerException” at run-time will not occur.
- It may contain a value or it will be empty.
- Optional Stream API contain methods such as “findAny”, “findFirst”, “max” and “min”
- In selenium it will be used in scenarios such as the “DropDownBox” or “ListBox”
Prerequisite:
package streamFilters;
public class Student {
private String student;
private int id;
public int result2;
public Student(String student, int id) {
this.student = student;
this.id = id;
}
public String getStudent() {
return student;
}
public void setStudent(String student) {
this.student = student;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
1. Optional Examples
package streamFilters;import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class OptionalEg{
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student("Kellby", 4),
new Student("rose", 5),
new Student("warner", 8),
new Student("RVS", 3),
new Student("SS", 13)
);
// Optional isPresent and ifPresent
Optional<Student> result1 = students.stream().filter(x -> "RVS".equals(x.getStudent())).findAny();
Optional<Student> result2 = students.stream().filter(y -> "SS".equals(y.getStudent())).findAny();
Optional<String> result3 = Optional.empty();
if (result1.isPresent()) {
System.out.println("Student name: "+ result1.get().getStudent()+" "+"Student ID: "+ result1.get().getId());
} else {
System.out.println("Value not available.");
}
result1.ifPresent(g -> System.out.println("Student name: " +result1.get().getStudent()+ " "+"Student ID: " +result1.get().getId()));
// E.g. found.orElse
String st1 = result1.orElse(new Student("No value",0)).getStudent();
String st2 = result2.orElse(new Student("No value",0)).getStudent();
System.out.println("The matched name is: "+st1);
System.out.println("The matched name is: "+st2);
// If input value is empty o/p value would be empty
System.out.println(result3.orElse("<Empty>"));
// Compound Conditions
Optional<Student> result5 = students.stream().filter((z) -> "RVS".equals(z.getStudent()) && 3 == z.getId()).findAny();
result5.ifPresent(g -> System.out.println("Student name: " +result5.get().getStudent()+ " "+"Student ID: " +result5.get().getId()));
// E.g findFirst(), max(), min()
Optional<Student> result4 = students.stream().findFirst();
System.out.println("First Student Name: "+ result4.get().getStudent() +" "+"First Student ID: "+ result4.get().getId());
Optional<Student> max = students.stream().max(comparing(students));
System.out.println("The Max student ID: " + max.get().getId());
Optional<Student> min = students.stream().min(comparing(students));
System.out.println("The Min student ID: " + min.get().getId());
}
private static Comparator<? super Student> comparing(Object object) {
Comparator <Student> comparator = (p1, p2) -> Integer.compare(p1.getId(), p2.getId());
return comparator;
}
}
Output:
Student name: RVS Student ID: 3
Student name: RVS Student ID: 3
The matched name is: RVS
The matched name is: SS
<Empty>
Student name: RVS Student ID: 3
First Student Name: Kellby First Student ID: 4
The Max student ID: 13
The Min student ID: 3
2. Optional Map
package streamFilters;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
public class FilterMap {
public static void main(String[] args) {
List<Student> students = Arrays.asList(
new Student("Kellby", 4),
new Student("rose", 5),
new Student("warner", 8),
new Student("RVS", 3),
new Student("SS", 13)
);
// Optional isPresent and ifPresent
Optional<String> result1 = students.stream().filter(x -> "RVS".equals(x.getStudent())).map(Student::getStudent).findAny();
Optional<Integer> result2 = students.stream().filter(y -> "RVS".equals(y.getStudent())).map(Student::getId).findAny();
Optional<String> result3 = Optional.empty();
if (result1.isPresent()) {
System.out.println("Student name1: "+result1+" "+"Student ID: "+result2);
} else {
System.out.println("Value not available.");
}
result1.ifPresent(g -> System.out.println("Student name1: "+result1+" "+"Student ID: "+result2));
// E.g. found.orElse
System.out.println("The matched name is: "+ result1.orElse("<Empty>") + " Name would be Empty if value not matched");
System.out.println("The matched id is: "+ result2.orElse(1) + " Id would be one if value not matched");
// As input value is empty o/p value would be empty
System.out.println("When Empty object is pass: " + result3.orElse("<Empty>"));
// E.g findFirst(), max(), min()
Optional<String> result4 = students.stream().map(Student::getStudent).findFirst();
Optional<Integer> result5 = students.stream().map(Student::getId).findFirst();
System.out.println("First Student name: "+ result4 + "First Student id: "+ result5);
Optional<Student> max = students.stream().max(comparing(students));
System.out.println("The Max student ID: " + max.map(Student::getId));
Optional<Student> min = students.stream().min(comparing(students));
System.out.println("The Min student ID: " + min.map(Student::getId));
}
private static Comparator<? super Student> comparing(Object object) {
Comparator <Student> comparator = (p1, p2) -> Integer.compare(p1.getId(), p2.getId());
return comparator;
}
}
Output:
Student name1: Optional[RVS] Student ID: Optional[3]
Student name1: Optional[RVS] Student ID: Optional[3]
The matched name is: RVS Name would be Empty if value not matched
The matched id is: 3 Id would be one if value not matched
When Empty object is pass: <Empty>
First Student name: Optional[Kellby]
First Student id: Optional[4]
The Max student ID: Optional[13]
The Min student ID: Optional[3]