Tuesday 4 July 2017

Selenium- java 8- Example of Streams filter(), findAny(), Maps and orElse() with compound conditions

Selenium- java 8- Example of Streams filter(), findAny(), Maps and orElse() with compound conditions


Selenium- java 8- Example of Streams filter(), findAny(), Maps and orElse() with compound conditions


1.
package streamFilters;

public class Student {

    private String student;
    private int id;

    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;
}
 
 

}


2. Java 8: stream.filter() to filter a List, and .findAny().orElse (null) to return an object conditional.

package streamFilters;

import java.util.Arrays;
import java.util.List;

public class FindAnyOrElseNull{


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)
       );

       Student result1 = students.stream()                      
               .filter(x -> "rose".equals(x.getStudent()))      
               .findAny()                                    
               .orElse(null);                                
     
if (result1==null){

System.out.println(result1);

       }else{
        System.out.println("Student name: "+result1.getStudent()+" "+"Student ID: "+result1.getId() );
       }

       System.out.println("Student name: "+result1.getStudent()+" "+"Student ID: "+result1.getId() );

       Student result2 = students.stream()
               .filter(x -> "SSS".equals(x.getStudent()))
               .findAny()
               .orElse(null);
     
if (result2==null){

System.out.println(result2);

       }else{
        System.out.println("Student name: "+result2.getStudent()+" "+"Student ID: "+result2.getId() );
       }

}


}

Output:

Student name: rose Student ID: 5
Null

3. Java 8:  Compound Conditions

package streamFilters;

import java.util.Arrays;
import java.util.List;


public class CompundConditions {

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)
        );
     
        Student result1 = students.stream()
                .filter((y) -> "SS".equals(y.getStudent()) && 13 == y.getId())
                .findAny()
                .orElse(null);

if (result1==null){

System.out.println(result1);

        }else{
        System.out.println("Student name: "+result1.getStudent()+" "+"Student ID: "+result1.getId() );
        }


        //or like this
        Student result2 = students.stream()
                .filter((z -> "warner1".equals(z.getStudent()) && 8 == z.getId()))
                .findAny()
                .orElse(null);

if (result2==null){

System.out.println(result2);

        }else{
        System.out.println("Student name: "+result2.getStudent()+" "+"Student ID: "+result2.getId() );
        }

    }


}

Output:

Student name: SS Student ID: 13
Null


4. Java8: Filter and Map


package streamFilters;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

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)
        );

        String name = students.stream()
                .filter(x -> "RVS".equals(x.getStudent()))
                .map(Student::getStudent)                      
                .findAny()
                .orElse("");

        System.out.println("Student name : " + name);

        List<String> collect = students.stream()
                .map(Student::getStudent)
                .collect(Collectors.toList());

        System.out.println("********************  List Names:");
        collect.forEach(System.out::println);

    }

}

Output:
Student name : RVS
********************  List Names:
Kellby
rose
warner
RVS
SS

No comments:

Post a Comment