7 Ways to Iterate Through ArrayList in Java Loop

Technology

In loop through arraylist java, Question: How to iterate through an arraylist Java? This Article signify the use of ArrayList, Iterator and a List.here learn to java arraylist, iterate and array Examples.

There are 7 ways you can loop through arraylist java.

  1. Simple For loop
  2. Enhanced For loop
  3. Iterator
  4. ListIterator
  5. While loop
  6. Iterable.forEach() util
  7. Stream.forEach() util

Also Read My Prev Article for convert a String to Char Array in Java and Paypal Payment Gateway Integration using Java

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

loop through arraylist java,how to iterate through an arraylist in java,iterate through list java,iterate list java	,java iterate through list,java iterate list,iterate through arraylist java,iterate through list java,how to iterate through an arraylist in java,loop through arraylist java,java loop through arraylist,java loop list,java loop through list
Iterate Through Loop in Java

Java Example:

You required to JDK 13 to execute following program as point-5 above uses simple Java include stream() util.

Example of loop through arraylist java

void

java.util.stream.Stream.forEach[php](Consumer<? super String> action) executes an action for each same element of Java program stream.</p>

[php]
package pakainfo.com.tutorials;
 
import java.util.*;
 
/**
 * @author Pakainfo.com
 * How to iterate through Java List? Seven (7) ways to Iterate Through Loop in Java.
 * 1. Simple For loop
 * 2. Enhanced For loop
 * 3. Iterator
 * 4. ListIterator
 * 5. While loop
 * 6. Iterable.forEach() util
 * 7. Stream.forEach() util
 */
 
public class PakainfoIterateThroughList {
 
    public static void main(String[] argv) {
 
        // create list
        List<String> pakainfoList = new ArrayList<String>();
 
        // add 4 different values to list
        pakainfoList.add("Loans");
        pakainfoList.add("Mortgage");
        pakainfoList.add("Attorney");
        pakainfoList.add("Lawyer");
 
        // Other way to define list is - we will not use this list :)
        List<String> pakainfoListNew = Arrays.asList("Loans", "Mortgage", "Attorney", "Lawyer");
 
        // Simple For loop
        System.out.println("==============> Way 1. Simple For loop Java Example.");
        for (int i = 0; i < pakainfoList.size(); i++) {
            System.out.println(pakainfoList.get(i));
        }
 
        // New Enhanced For loop
        System.out.println("\n==============> Way 2. New Enhanced For loop Java Example..");
        for (String temp : pakainfoList) {
            System.out.println(temp);
        }
 
        // Iterator - Returns an iterator over the elements in this list in proper sequence.
        System.out.println("\n==============> Way 3. Iterator Java Example...");
        Iterator<String> pakainfoIterator = pakainfoList.iterator();
        while (pakainfoIterator.hasNext()) {
            System.out.println(pakainfoIterator.next());
        }
 
        // ListIterator - traverse a list of elements in either forward or backward order
        // An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration,
        // and obtain the iterator's current position in the list.
        System.out.println("\n==============> Way 4. ListIterator Java Example...");
        ListIterator<String> pakainfoListIterator = pakainfoList.listIterator();
        while (pakainfoListIterator.hasNext()) {
            System.out.println(pakainfoListIterator.next());
        }
 
        // while loop
        System.out.println("\n==============> Way 5. While Loop Java Example....");
        int i = 0;
        while (i < pakainfoList.size()) {
            System.out.println(pakainfoList.get(i));
            i++;
        }
 
        // Iterable.forEach() util: Returns a sequential Stream with this collection as its source
        System.out.println("\n==============> Way 6. Iterable.forEach() Java Example....");
        pakainfoList.forEach((temp) -> {
            System.out.println(temp);
        });
 
        // collection Stream.forEach() util: Returns a sequential Stream with this collection as its source
        System.out.println("\n==============> Way 7. Stream.forEach() Java Example....");
        pakainfoList.stream().forEach((pakainfoTemp) -> System.out.println(pakainfoTemp));
    }
}

Output:

==============> Way 1. Simple For loop Java Example.
Loans
Mortgage
Attorney
Lawyer
 
==============> Way 2. New Enhanced For loop Java Example..
Loans
Mortgage
Attorney
Lawyer
 
==============> Way 3. Iterator Java Example...
Loans
Mortgage
Attorney
Lawyer
 
==============> Way 4. ListIterator Java Example...
Loans
Mortgage
Attorney
Lawyer
 
==============> Way 5. While Loop Java Example....
Loans
Mortgage
Attorney
Lawyer
 
==============> Way 6. Iterable.forEach() Java Example....
Loans
Mortgage
Attorney
Lawyer
 
==============> Way 7. Stream.forEach() Java Example....
Loans
Mortgage
Attorney
Lawyer
 
Process finished with exit code 0

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about How to iterate through Java List? Seven (7) ways to Iterate Through Loop in Java.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Google API Line Chart with JSON PHP & Mysql
  2.   PHP CodeIgniter 3 Validation Example Tutorial From Scratch
  3.   Remove Last Comma string in javascript
  4.   Http Post Method using Angular Example
  5.   laravel 5/6/7 open link in new tab Example
  6.   PHP Time Ago Helper Function
  7.   JavaScript JSON Encoder and Decoder Example
  8.   Login with LinkedIn using JavaScript SDK
  9.   ReactJS State Example Tutorial
  10.   How to convert a String to Char Array in Java?

Leave a Reply

Your email address will not be published. Required fields are marked *