Javatpoint Logo
Javatpoint Logo

How to Iterate List in Java

In Java, List is is an interface of the Collection framework. It provides us to maintain the ordered collection of objects. The implementation classes of List interface are ArrayList, LinkedList, Stack, and Vector. The ArrayList and LinkedList are widely used in Java. In this section, we will learn how to iterate a List in Java. Throughout this section, we will use ArrayList.

Java for Loop

  1. Basic for Loop
  2. Enhanced for Loop

Java Iterators

  1. Iterator
  2. ListIterator

Java forEach Method

  1. Iterable.forEach()
  2. Stream.forEach()

Java for Loop

Basic for Loop

Java for loop is the most common flow control loop for iteration. The for loop contains a variable that acts as an index number. It executes until the whole List does not iterate.

Syntax:

IterateListExample1.java

Output

Boston
San Diego
Las Vegas
Houston
Miami
Austin

Enhanced for Loop

It is similar to the basic for loop. It is compact, easy, and readable. It is widely used to perform traversal over the List. It is easy in comparison to basic for loop.

Syntax:

IterateListExample2.java

Output

Boston
San Diego
Las Vegas
Houston
Miami
Austin

Java Iterator

Iterator

Java provides an interface Iterator to iterate over the Collections, such as List, Map, etc. It contains two key methods next() and hasNaxt() that allows us to perform an iteration over the List.

next(): The next() method perform the iteration in forward order. It returns the next element in the List. It throws NoSuchElementException if the iteration does not contain the next element in the List. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous() to go back and forth.

Syntax:

hasNext(): The hasNext() method helps us to find the last element of the List. It checks if the List has the next element or not. If the hasNext() method gets the element during traversing in the forward direction, returns true, else returns false and terminate the execution.

Syntax:

IterateListExample3.java

Output

Boston
San Diego
Las Vegas
Houston
Miami
Austin

ListIterator

The ListIterator is also an interface that belongs to java.util package. It extends Iterator<E> interface. It allows us to iterate over the List either in forward or backward order. The forward iteration over the List provides the same mechanism, as used by the Iterator. We use the next() and hasNext() method of the Iterator interface to iterate over the List.

IterateListExample4.java

Output

Boston
San Diego
Las Vegas
Houston
Miami
Austin

Java forEach Method

Iterable.forEach()

The Iterable interface provides forEach() method to iterate over the List. It is available since Java 8. It performs the specified action for each element until all elements have been processed or the action throws an exception. It also accepts Lambda expressions as a parameter.

Syntax:

The default implementation behaves like:

It accepts action as a parameter that is non-interfering (means that the data source is not modified at all during the execution of the stream pipeline) action to perform on the elements. It throws NullPointerException if the specified action is null.

The Consumer<T> is a functional interface that can be used as the assignment target for a lambda expression or method reference. T is the type of input to the operation. It represents an operation that accepts a single input argument and returns no result.

IterateListExample5.java

Output

Boston
San Diego
Las Vegas
Houston
Miami
Austin

Stream.forEach()

Java Stream interface allows us to convert the List values into a stream. With the help of Stream interface we can access operations like forEach(), map(), and filter().

Syntax:

It accepts action as a parameter that is non-interfering (means that the data source is not modified at all during the execution of the stream pipeline) action to perform on the elements.

The Consumer<T> is a functional interface that can be used as the assignment target for a lambda expression or method reference. T is the type of input to the operation. It represents an operation that accepts a single input argument and returns no result.

IterateListExample5.java

Output

Boston
San Diego
Las Vegas
Houston
Miami
Austin





Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA