
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to iterate over a list in Java?
The List interface extends Collection interface and represents a collection storing a sequence of elements. User of a list has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most popular implementation of the List interface among the Java developers.
There are multiple ways to iterate a Java List. Following are some of the ways −
Way #1
Use for/while loop to iterate a List and get element by index.
for(int i= 0; i < list.size(); i++) { System.out.println(list.get(i)); }
Way #2
Use foreach loop to iterate list of elements.
for (Integer integer : list) { System.out.print(integer + " "); }
Way #3
Use iterator from the list to iterate through its elements.
Iterator<Integer> iterator = list.iterator(); while(iterator.hasNext()) { System.out.print(iterator.next() + " "); }
Way #4
Use listIterator from the list to iterate through its elements.
Iterator<Integer> iterator = list.listIterator(); while(iterator.hasNext()) { System.out.print(iterator.next() + " "); }
Way #5
Use forEach of the list to iterate through its elements.
list.forEach(i -> {System.out.print(i + " ");});
Way #6
Use forEach of the stream of list to iterate through its elements.
list.stream().forEach(i -> {System.out.print(i + " ");});
Now let's explore above ways to iterate a list in the examples given below −
Example 1
Following is the example showing loop and iterator methods to iterate a list −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5)); for(int i= 0; i< list.size(); i++) { System.out.print(list.get(i) + " "); } System.out.println(); for (Integer integer : list) { System.out.print(integer + " "); } Iterator<Integer> iterator = list.iterator(); System.out.println(); while(iterator.hasNext()) { System.out.print(iterator.next() + " "); } } }
Output
This will produce the following result −
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Example 2
Following is the example showing various methods to iterate a list −
package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5)); Iterator<Integer> iterator1 = list.listIterator(); while(iterator1.hasNext()) { System.out.print(iterator1.next() + " "); } System.out.println(); list.forEach(i -> {System.out.print(i + " ");}); System.out.println(); list.stream().forEach(i -> {System.out.print(i + " ");}); } }
Output
This will produce the following result −
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
- Related Articles
- How to iterate over a Java list?
- How to iterate over a C# list?
- Program to iterate over a List using Java 8 Lambda
- Iterate over a list in Python
- How to iterate a list in Java?
- How to iterate a Java List?
- Java Program to Iterate over a HashMap
- Java Program to Iterate over a Set
- How to iterate over a list of files with spaces in Linux?
- Java Program to Iterate over enum
- How to iterate through Java List?
- How to iterate a Java List using Iterator?
- How to iterate over a Hashmap in Kotlin?
- Java Program to Iterate over an ArrayList
- How to iterate over a C# dictionary?
