Java ArrayList forEach() Method
What is an ArrayList?
Arraylist is a part of collection framework in java. It contains some inbuilt function like forEach() function which can be found in the java.util.ArrayList Package.
It implements the List interface of the collections framework.
Here, in the page we will discuss about the java ArrayList’s forEach() Method .
Java ArrayList forEach() Method :
Until all elements have been processed or the action throws an exception, the given action is carried out on each Iterable element. Actions are carried out in the iteration order (if one is specified), unless the implementing class specifies otherwise. The action relays any exceptions it throws to the caller.
Syntax :
public void forEach(Consumer super E> action)
clear() Function in ArrayList
clone() Function in ArrayList
add() Function in ArrayList
addall() Function in ArrayList
Example 1 :
import java.util.*; public class Main{ public static void main(String[] args) { //ArrayList object creation ArrayList<String> list = new ArrayList<>(); //Adding elements to the list list.add("A"); list.add("B"); list.add("C"); list.add("D"); list.add("E"); list.add("F"); //Printing the initial list. System.out.println("ArrayList : " + list); //Using forEach() method to iterate through the list. list.forEach((n) -> System.out.println(n)); } }
Output :
ArrayList : [A, B, C, D, E, F] A B C D E F
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment