HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Flow Control / Enhanced for loop

Java for-each Loop

Java 5 introduced an for-each loop, which is called a enhanced for each loop. It is used to iterate over elements of an array and the collection.

for-each loop is a shortcut version of for-loop which skips the need to get the iterator and loop over iterator using it’s hasNext() and next() method.

1. Java for-each loop syntax

The general syntax for a for-each loop is as follows:

for(T element : a_collection_or_an_array_of_type_T) 
{
        // This code will be executed once for each element in the collection/array.
        // Each time this code is executed, the element variable holds the reference
        // of the current element in the collection/array
}

2. Java for-each loop example – iterate over array

For example, following snippet of code prints all elements of an integer array numArray.

int[] numArray = {10, 20, 30, 40};

for(int num : numArray) 
{
        System.out.println(num);
}

Program Output.

10
20
30
40

3. Java for each loop example – iterate over collection

Similarly, for a collection type also, program will be written same.

public static void main(String[] args) 
{
	List<Integer> numList = new ArrayList<Integer>();

	numList.add(10);
	numList.add(20);
	numList.add(30);
	numList.add(40);

        //foreach loop
	for(int num : numList) 
        {
	        System.out.println(num);
	}
}

Program Output.

10
20
30
40

4. for each loop with lambda

Since Java 8, we can use lambda expressions as well for a collection type.

Java program to iterate over a list using for each loop and lambda.

public static void main(String[] args) 
{
	List<Integer> numList = new ArrayList<Integer>();

	numList.add(10);
	numList.add(20);
	numList.add(30);
	numList.add(40);

        //foreach loop with lambda
	numList.forEach( item -> System.out.println(item) );

        //Pass function reference
        numList.forEach( System.out::println );
}

Program Output.

10
20
30
40

Drop me your questions related to for-each loop control statement in Java.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Flow Control

  • Java Flow Control Statements
  • Java if-else Statement
  • Java Switch Statement
  • Java for Loop
  • Java for-each Loop
  • Java while Loop
  • Java do-while Loop
  • Java break Statement
  • Java continue Statement
  • Java Labeled Statement

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces