Java ArrayList forEach() method with example

Java ArrayList forEach() method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of iteration (if an iteration order is specified). Exceptions thrown by the action are relayed to the caller.

Method Signature

public void forEach(Consumer<? super E> action)

Specified by:

forEach method in interface Iterable

Parameters:

action – The action to be performed for each element

ArrayList forEach() method implementation

public void forEach(Consumer<? super E> action) {
        Objects.requireNonNull(action);
        final int expectedModCount = modCount;
        @SuppressWarnings("unchecked")
        final E[] elementData = (E[]) this.elementData;
        final int size = this.size;
        for (int i=0; modCount == expectedModCount && i < size; i++) {
            action.accept(elementData[i]);
        }
        if (modCount != expectedModCount) {
            throw new ConcurrentModificationException();
        }
    }

Java ArrayList forEach() method example

package com.ehowtonow.java.util.arraylist;

import java.util.ArrayList;

public class ForEachExample {

	public static void main(String[] args) {

		// create empty array list with Integer type
		ArrayList<Integer> numericList = new ArrayList<Integer>(2);

		// add() method to add Integer element in the list
		numericList.add(10);
		numericList.add(15);
		numericList.add(20);
		numericList.add(25);
		numericList.add(30);

		/*
		 * forEach method added to ArrayList in Java 8 release, which print iterate the
		 * list
		 */
		numericList.forEach(System.out::print);

		numericList.forEach(n -> System.out.println("Java 8 forEach : " + n));

		numericList.forEach(n -> {
			System.out.println("Java 8 forEach 1 : " + n);
		});

	}

}

Output

1015202530Java 8 forEach : 10
Java 8 forEach : 15
Java 8 forEach : 20
Java 8 forEach : 25
Java 8 forEach : 30
Java 8 forEach 1 : 10
Java 8 forEach 1 : 15
Java 8 forEach 1 : 20
Java 8 forEach 1 : 25
Java 8 forEach 1 : 30


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. Java ArrayList removeAll() method with example
  2. Java ArrayList add(int index, E element) method with example
  3. Java ArrayList indexOf() method with example
  4. Java ArrayList add(E e) method with example
  5. Java ArrayList addAll(Collection< ? extends E> c) method with example
  6. Java ArrayList ensureCapacity() method with example
  7. Java ArrayList addAll(int index, Collection c) method with example
  8. Java ArrayList clone() method with example
  9. Java ArrayList remove(Object o) method with example
  10. Java ArrayList forEach() method with example
  11. Java ArrayList isEmpty() method with example
  12. Java ArrayList clear() method with example
  13. Java ArrayList remove() method with example
  14. Java ArrayList contains() method with example
  15. Java ArrayList get() method with example

Be the first to comment

Leave a Reply