How to do it...

Follow these steps to implement the example:

  1. First, we will implement some auxiliary classes we will use in the example. Create a class named Person with the basic characteristics of a person. Check the Creating streams from different sources recipe to see the source code of this class.
  2. As we'll work with methods that depend on the order of the elements of the stream, we have to override some methods in the Person class. First, we'll override the compareTo() method that compares two persons. We'll create a static Comparator object using the Comparator interface to compare two Person objects using their first name and last name. Then, we'll use that comparator in the compareTo() method:
        private static Comparator<Person> comparator=Comparator
.comparing(Person::getLastName)
.thenComparing(Person::getFirstName);

@Override
public int compareTo(Person otherPerson) {
return comparator.compare(this, otherPerson);
}
  1. Then, we override the equals() method that determines if two Person objects are equal. As we made in the compareTo() method, we use the Comparator static object we have created before.
        @Override 
public boolean equals(Object object) {
return this.compareTo((Person)object)==0;
}
  1. Finally, we override the hashCode() method that calculates a hash value for a Person object. In Java, equal objects must produce the same hash code, so we have to override this method and generate the hash code of a Person object using the first name and last name attributes and the hash() method of the Objects class:
        public int hashCode() { 
String sequence=this.getLastName()+this.getFirstName();
return sequence.hashCode();
}
  1. In this example, we will also use the PersonGenerator and DoubleGenerator classes used in the Creating streams from different sources recipe.
  2. Now, create the Main class with the main() method. First, we create a List of ten random Person objects:
        public class Main { 

public static void main(String[] args) {
List<Person> persons=PersonGenerator.generatePersonList(10);
  1. Then, we'll use the forEach() method to write the names of all the persons of the generated list. The forEach() method receives as parameter the expression we want to apply to each element. In our case, we use a lambda expression to write the information to the console:
        persons.parallelStream().forEach(p -> { 
System.out.printf("%s, %s ", p.getLastName(),
p.getFirstName());
});
  1. Then, you'll learn how to apply an action to each element in an ordered way. First, we create a list of random Double numbers using the DoubleGenerator class. Then, we create a parallel stream, sort the elements of the stream using the sorted() method, and then use the forEachOrdered() method to write the numbers to the console in an ordered way:
        List<Double> doubles= DoubleGenerator.generateDoubleList(10, 100); 
System.out.printf("Parallel forEachOrdered() with numbers ");
doubles.parallelStream().sorted().forEachOrdered(n -> {
System.out.printf("%f ",n);
});
  1. Now, let's see what happens if you sort the elements of the stream but don't use the forEachOrdered() method. Repeat the same sentence as before but use the forEach() method instead:
        System.out.printf("Parallel forEach() after sorted()
with numbers ");
doubles.parallelStream().sorted().forEach(n -> {
System.out.printf("%f ",n);
});
  1. Then, we'll test how the forEachOrdered() method works with a stream of Person objects:
        persons.parallelStream().sorted().forEachOrdered( p -> { 
System.out.printf("%s, %s ", p.getLastName(),
p.getFirstName());
});
  1. Finally, let's test the peek() method. This method is similar to the forEach() method, but it's an intermediate operation. It's normally used for log purposes:
        doubles 
.parallelStream()
.peek(d -> System.out.printf("Step 1: Number: %f ",d))
.peek(d -> System.out.printf("Step 2: Number: %f ",d))
.forEach(d -> System.out.printf("Final Step: Number: %f ",d));
..................Content has been hidden....................

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