How to do it...

Follow these steps to implement the example:

  1. First, we'll implement some auxiliary classes we will use in the example. First, implement the Person class, which stores the basic attributes of a person, and the PersonGenerator class, which generates a List of random Person objects. Please, check the recipe Apply an action to all the elements of a stream to see the source code of both classes.
  2. Then, create the Main class with the main() method. First, we'll create a List of random Person objects using the PersonGenerator class:
        public class Main { 
public static void main(String[] args) {
List<Person> persons=PersonGenerator.generatePersonList(10);
  1. Then, calculate the maximum and minimum values of the salary field to verify that all our calculations are correct. We use two streams for the calculation, the first one with the map() and max() methods and the second one with the mapToInt() and min() methods:
        int maxSalary = persons.parallelStream().map(p -> p.getSalary())
.max(Integer::compare).get();
int minSalary = persons.parallelStream().mapToInt(p -> p
.getSalary()).min().getAsInt();
System.out.printf("Salaries are between %d and %d ", minSalary,
maxSalary);
  1. Now, we'll test some conditions. First, let's verify that all the Person objects generated have a salary greater than zero with the allMatch() method and the corresponding lambda expression:
        boolean condition; 
condition=persons.parallelStream().allMatch
(p -> p.getSalary() > 0);
System.out.printf("Salary > 0: %b ", condition);
  1. We repeat the condition to test if all the salaries are greater than 10,000 and 30,000.
        condition=persons.parallelStream().allMatch
(p -> p.getSalary() > 10000);
System.out.printf("Salary > 10000: %b ",condition);
condition=persons.parallelStream().allMatch
(p -> p.getSalary() > 30000);
System.out.printf("Salary > 30000: %b ",condition);
  1. Then, we'll use the anyMatch() method to test if there is someone with a salary greater than 50,000 and 100,000:
        condition=persons.parallelStream().anyMatch
(p -> p.getSalary() > 50000);
System.out.printf("Any with salary > 50000: %b ",condition);
condition=persons.parallelStream().anyMatch
(p -> p.getSalary() > 100000);
System.out.printf("Any with salary > 100000: %b ",condition);
  1. To finish this block of tests, we use the noneMatch() method to verify that there's none with a salary greater than 100,000
        condition=persons.parallelStream().noneMatch
(p -> p.getSalary() > 100000);
System.out.printf("None with salary > 100000: %b ",condition);
  1. After that, we use the findAny() method to get a random element of the stream of Person objects:
        Person person = persons.parallelStream().findAny().get(); 
System.out.printf("Any: %s %s: %d ", person.getFirstName(),
person.getLastName(), person.getSalary());
  1. Then, we use the findFirst() method to get the first element of the stream of Person objects:
        person = persons.parallelStream().findFirst().get(); 
System.out.printf("First: %s %s: %d ", person.getFirstName(),
person.getLastName(), person.getSalary());
  1. Finally, we sort the stream by salary using the sorted() method, passing Comparator expressed as a lambda expression, and use the findFirst() method to obtain, in this case, the Person object with the lowest salary:
        person = persons.parallelStream().sorted((p1,p2) -> {  
return p1.getSalary() - p2.getSalary();
}).findFirst().get();
System.out.printf("First Sorted: %s %s: %d ",
person.getFirstName(), person.getLastName(),
person.getSalary());
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.199.191