Here we will see how the forEach work in the background. The forEach is a method that is declared in
the Stream Interface. The Consumer Interface is a parameter for this method. In the Consumer Interface we have the accept
method that must be implemented.
// The forEach method declaration and Consumer Interface as parameter.
void forEach(Consumer super T> action);
// The accept method in the Consumer Interface that must be implemented.
void accept(T t);
package com.Kruger.bdg;
import com.sun.tools.javac.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
// This is the original lambda expression
List.of( 1, 2, 3, 4 ).stream()
.filter(num -> num%2 == 0)
.forEach(val -> System.out.println(val));
// Create line to separate output
System.out.println();
// Here we use the Consumer implementation
List.of( 1, 2, 3, 4 ).stream()
.filter(num -> num%2 == 0)
.forEach(new ConsumerImp());
}
}
class ConsumerImp implements Consumer{
// Implements the accept method
@Override
public void accept(Integer integer) {
System.out.println(integer);
}
}
In this example we will look how the Stream map work in the background. As can seen below map is
an method in the Stream Interface. It is using the Interface Function as a paramter, apply
method must be implemented.
This is the only method that is not been implemented. Therefore it can be used in lambdas.
// This is the map method in the Stream Interface, Function is a parameter
/**
* Returns a stream consisting of the results of applying the given
* function to the elements of this stream.
*
* The element type of the new stream
* mapper
* function to apply to each element
* return the new stream
*/
Stream map(Function super T, ? extends R> mapper);
// This is the apply method in the Function Interface that must be implemented.
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
package com.Kruger.bdg;
import com.sun.tools.javac.util.List;
import java.util.function.Function;
public class Main {
public static void main(String[] args) {
// This is the original lambda expression
List.of( 1, 2, 3, 4 ).stream()
.filter(num -> num%2 == 0)
.map( x -> x * x)
.forEach(val -> System.out.println(val));
// Create line to separate output
System.out.println();
// Here we use the Function implementation
List.of( 1, 2, 3, 4 ).stream()
.filter(num -> num%2 == 0)
.map(new ImpFunction())
.forEach(val -> System.out.println(val));
}
}
class ImpFunction implements Function{
@Override
public Integer apply(Integer integer) {
return integer * integer;
}
}
In this example we will look how the Stream filter work in the background. As can seen below filter is
an method in the Stream Interface. It is using the Interface Predicate as a paramter, test
method must be implemented.
This is the only method that is not been implemented. Therefore it can be used in lambdas.
/**
* Returns a stream consisting of the elements of this stream that match
* the given predicate.
* Predicate to apply to each element to determine if it
* should be included
* return the new stream
*/
// This is the filter method declaration in the Stream Interface
Stream filter(Predicate super T> predicate);
/**
* Evaluates this predicate on the given argument.
*
* param t the input argument
* return true if the input argument matches the predicate,
* otherwise false
*/
// This is the test method declaration in the Predicate Interface that must be implemented.
boolean test(T t);
package com.Kruger.bdg;
import com.sun.tools.javac.util.List;
import java.util.function.Predicate;
public class Main {
public static void main(String[] args) {
// This is the original lambda expression
List.of( 1, 2, 3, 4 ).stream()
.filter(num -> num%2 == 0)
.forEach(val -> System.out.println(val));
// Create line to separate output
System.out.println();
// Here we use the Predicate implementation
List.of( 1, 2, 3, 4 ).stream()
.filter(new PredicateImp())
.forEach(val -> System.out.println(val));
}
}
// Here we create a class that implement the Predicate
class PredicateImp implements Predicate{
// We must implement the test method
@Override
public boolean test(Integer integer) {
return integer%2 == 0;
}
}