Java ArrayList forEach() method with example


Warning: mysqli_query(): (HY000/1030): Got error 28 from storage engine in /home/ehowtono/public_html/wp-includes/wp-db.php on line 2030

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. Thirukural – Therindhuvinaiyaatal – Kural 518
  2. Java – Scanner Class
  3. TNPSC Study Materials General English 2
  4. TNPSC Study Materials Indian Polity Tamil 26
  5. Thirukural – Piranil Vizhaiyaamai – Kural 149
  6. How to replace the element from List
  7. Thirukural – Nalkuravu – Kural 1041 – 1050
  8. Thirukural – Natpu – Kural 781
  9. TNPSC Motor Vehicle Inspector Grade-2 2012 Automobile Engineering Question And Answer PDF
  10. Milliseconds to Minutes conversion online
  11. Thirukural – Matiyinmai – Kural 606
  12. Thirukural – Pirivaatraamai – Kural 1151 – 1160
  13. TNPSC Draughtsman Grade-3 In Town And Country Planning Dept 2012 Architectural Assistantship Question And Answer PDF
  14. TNPSC Study Materials Geography Tamil 15
  15. திருக்குறள் – நீத்தார் பெருமை – குறள் 23

Be the first to comment

Leave a Reply