How to Iterate through Collection Objects in Java?
Last Updated :
07 Jan, 2021
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?
- Using enhanced For loop
- Using Iterator method
- Using Simple For loop
- Using forEach method
Method 1: Using enhanced For loop
Syntax used :
for (datatype variable : collection_used)
Example:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Collection<String> gfg = new ArrayList<String>();
gfg.add( "Abhishek Rout" );
gfg.add( "Vaibhav Kamble" );
gfg.add( "Anupam Kumar" );
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
LinkedList<String> gfg = new LinkedList<String>();
gfg.add( "Abhishek Rout" );
gfg.add( "Vaibhav Kamble" );
gfg.add( "Anupam Kumar" );
System.out.println( "Using For loop" );
for (Iterator<String> name = gfg.iterator();
name.hasNext();)
System.out.println( "Name : " + name.next());
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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
Vector<String> gfg = new Vector<String>();
gfg.add( "Abhishek Rout" );
gfg.add( "Vaibhav Kamble" );
gfg.add( "Anupam Kumar" );
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 :
collection_used.forEach((data_type iterating_variable) -> { System.out.println(iterating_variable); });
- Without iterable variable
collection_used.forEach(System.out::println);
Example:
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
ArrayList<String> gfg = new ArrayList<String>();
gfg.add( "Abhishek Rout" );
gfg.add( "Vaibhav Kamble" );
gfg.add( "Anupam Kumar" );
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
Please Login to comment...