Open In App

How to Iterate through Collection Objects in Java?

Last Updated : 07 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Any group of individual objects which are represented as a single unit is known as the Collection of the objects. In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all the collection classes and interface in it.

The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes.

How to iterate through Collection Objects?

  1. Using enhanced For loop
  2. Using Iterator method
  3. Using Simple For loop
  4. Using forEach method

Method 1: Using enhanced For loop 

Syntax used :

for (datatype variable : collection_used)

Example:

Java




// Java program to demonstrate the
// working of enhanced for loop to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the ArrayList
        Collection<String> gfg = new ArrayList<String>();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // for-each loop for iterating
        // unconditionally through collection
        for (String name : gfg)
            System.out.println("Name : " + name);
    }
}


Output

Name : Abhishek Rout
Name : Vaibhav Kamble
Name : Anupam Kumar

Method 2: Using Iterator method 

Syntax used :

for (Iterator variable = collection.iterator(); variable.hasNext();)

Example:

Java




// Java program to demonstrate the
// working of iterator method to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the LinkedList
        LinkedList<String> gfg = new LinkedList<String>();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // for loop for iterating
        // conditionally through collection
        System.out.println("Using For loop");
        
        for (Iterator<String> name = gfg.iterator();
             name.hasNext();)
            
            System.out.println("Name : " + name.next());
  
          // while loop for iterating
        // conditionally through collection
        System.out.println("\nUsing While Loop");
        
        Iterator<String> name = gfg.iterator();
  
        while (name.hasNext())
            System.out.println("Name : " + name.next());
    }
}


Output

Using For loop
Name : Abhishek Rout
Name : Vaibhav Kamble
Name : Anupam Kumar

Using While Loop
Name : Abhishek Rout
Name : Vaibhav Kamble
Name : Anupam Kumar

Method 3: Using Simple For loop 

Syntax used :

for (int i = 0; i < collection_used.length; i++)

Example:

Java




// Java program to demonstrate the
// working of for loop to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the ArrayList
        Vector<String> gfg = new Vector<String>();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // for loop for iterating
        // through collection
        for (int i = 0; i < gfg.size(); i++)
            System.out.println("Name " + (i + 1) + ": "
                               + gfg.get(i));
    }
}


Output

Name 1: Abhishek Rout
Name 2: Vaibhav Kamble
Name 3: Anupam Kumar

Method 4: Using forEach method 

forEach() method is available in Java 8 and each collection has this method that implements the iteration internally.

Syntax used :

  • With iterable variable
collection_used.forEach((data_type iterating_variable) -> { System.out.println(iterating_variable); });
  • Without iterable variable
collection_used.forEach(System.out::println);

Example:

Java




// Java program to demonstrate the
// working of forEach method to iterate
// collection objects 
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declaring the ArrayList
        ArrayList<String> gfg = new ArrayList<String>();
  
        // Appending new elements at
        // the end of the list
        gfg.add("Abhishek Rout");
        gfg.add("Vaibhav Kamble");
        gfg.add("Anupam Kumar");
  
        // forEach for iterating
        // through collection
        // with iterable variable
        System.out.println("With iterable");
        
        gfg.forEach((String name) -> {
            System.out.println("Name : " + name);
        });
  
        System.out.println("\nWithout iterable");
        gfg.forEach(System.out::println);
    }
}


Output

With iterable
Name : Abhishek Rout
Name : Vaibhav Kamble
Name : Anupam Kumar

Without iterable
Abhishek Rout
Vaibhav Kamble
Anupam Kumar


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads