forEach() Method Example In Java Stream

The forEach() method is a default method defined in the Iterable and Stream interface. It is a terminal operation in stream API. The purpose of the forEach() method is iterating the elements one by one of a collection to create a result. The forEach() method accept a single argument which is a functional interface. So, you can pass a lambda expression in argument.

  1. The forEach() method is a default method in the Iterable and Stream interface.
  2. The forEach() method is a terminal operation.
  3. The forEach() method iterates the elements one by one of a collection to produce a result.
  4. The forEach() method accept one single argument which is a functional interface.
  5. We can pass a lambda expression in argument.

Syntax:

 default void forEach(Consumer<super T>action)  

Example of forEach() method in Java 8

//Importing required classes
import java.util.ArrayList;
import java.util.List;

public class ForEachExample {
  public static void main(String args[]){
    //Declaring List
    List<String> countryList = new ArrayList< String >();
    countryList.add( "Afghanistan" );
    countryList.add( "Albania" );
    countryList.add( "Algeria" );
    countryList.add( "Australia" );
    countryList.add( "India" );
    countryList.add( "Brazil" );
    countryList.add( "Japan" );
    countryList.add( "Italy" );

    //forEach method to printing country names
    countryList.forEach( country -> System.out.println( country ) );
   
  }
}

Output:
    Afghanistan
    Albania
    Algeria
    India
    Australia
    Brazil
    Japan
    Italy

Example of forEach() method in Java 8

//Importing required classes
import java.util.ArrayList;
import java.util.List;

public class ForEachExampleWithFilter {
  public static void main(String args[]){
    //Declaring List
    List<Integer> intList = new ArrayList< Integer >();
    intList.add( 100 );
    intList.add( 99 );
    intList.add( 78 );
    intList.add( 3 );
    intList.add( 6 );
    intList.add( 8 );
    intList.add( 50 );
    intList.add( 55 );

    //forEach method to printing even numbers
    intList.stream()
        .filter(i -> i%2==0)
        //method reference
        .forEach( System.out :: println );
  }
}

Output:
    100
    78
    6
    8
    50

Example of Java Stream forEach method:

public class Students {
    private Integer age;
    private String name;

    Students(Integer age,String name){
        this.age=age;
        this.name=name;
    }
    //toString() method for printing
    public String toString() {
        return this.age + ", "+ this.name;
    }
}


//Importing required classes
import java.util.*;
import java.io.*;
import java.util.stream.*;

public class SortedExampleWithComparator {
  public static void main (String args[]){
    List<Students> studentsList = new ArrayList< Students >();
    studentsList.add(new Students( 18, "Pankaj" ));
    studentsList.add(new Students( 20, "Prakash" ));
    studentsList.add(new Students( 16, "Karim" ));
    studentsList.add(new Students( 19, "Shyam" ));
    
    studentsList
        .stream()
        //filtering the studentsList with age greater than 15 
        .filter(s -> s.age>15)
        //forEach with method reference
        .forEach(System.out::println);   
 }   
}

Output:
    20, Prakash
    16, Karim
    19, Tejas