Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

How to iterate over a list in Java?

// 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));
}
//Use foreach loop to iterate list of elements. 
for(int i= 0; i < list.size(); i++) {
   System.out.println(list.get(i));
}
//Use iterator from the list to iterate through its elements.
Iterator<Integer> iterator = list.iterator();
while(iterator.hasNext()) {
   System.out.print(iterator.next() + " ");
}
// Use listIterator from the list to iterate through its elements.
Iterator<Integer> iterator = list.listIterator();
while(iterator.hasNext()) {
   System.out.print(iterator.next() + " ");
}
// Use forEach of the list to iterate through its elements.
list.forEach(i -> {System.out.print(i + " ");});
//Use forEach of the stream of list to iterate through its elements.
list.stream().forEach(i -> {System.out.print(i + " ");});
Comment

listin looping in java

Iterator itr=list.iterator();
while(itr.hasNext()){
 system.out.println(itr.next); 
}
Comment

loop list java

package crunchify.com.tutorials;
 
import java.util.*;
 
/**
 * @author Crunchify.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 CrunchifyIterateThroughList {
 
    public static void main(String[] argv) {
 
        // create list
        List<String> crunchifyList = new ArrayList<String>();
 
        // add 4 different values to list
        crunchifyList.add("Facebook");
        crunchifyList.add("Paypal");
        crunchifyList.add("Google");
        crunchifyList.add("Yahoo");
 
        // Other way to define list is - we will not use this list :)
        List<String> crunchifyListNew = Arrays.asList("Facebook", "Paypal", "Google", "Yahoo");
 
        // Simple For loop
        System.out.println("==============> 1. Simple For loop Example.");
        for (int i = 0; i < crunchifyList.size(); i++) {
            System.out.println(crunchifyList.get(i));
        }
 
        // New Enhanced For loop
        System.out.println("
==============> 2. New Enhanced For loop Example..");
        for (String temp : crunchifyList) {
            System.out.println(temp);
        }
 
        // Iterator - Returns an iterator over the elements in this list in proper sequence.
        System.out.println("
==============> 3. Iterator Example...");
        Iterator<String> crunchifyIterator = crunchifyList.iterator();
        while (crunchifyIterator.hasNext()) {
            System.out.println(crunchifyIterator.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("
==============> 4. ListIterator Example...");
        ListIterator<String> crunchifyListIterator = crunchifyList.listIterator();
        while (crunchifyListIterator.hasNext()) {
            System.out.println(crunchifyListIterator.next());
        }
 
        // while loop
        System.out.println("
==============> 5. While Loop Example....");
        int i = 0;
        while (i < crunchifyList.size()) {
            System.out.println(crunchifyList.get(i));
            i++;
        }
 
        // Iterable.forEach() util: Returns a sequential Stream with this collection as its source
        System.out.println("
==============> 6. Iterable.forEach() Example....");
        crunchifyList.forEach((temp) -> {
            System.out.println(temp);
        });
 
        // collection Stream.forEach() util: Returns a sequential Stream with this collection as its source
        System.out.println("
==============> 7. Stream.forEach() Example....");
        crunchifyList.stream().forEach((crunchifyTemp) -> System.out.println(crunchifyTemp));
    }
}
Comment

java iterate over list

for (int i = 0; i < crunchifyList.size(); i++) {
            System.out.println(crunchifyList.get(i));
        }
Comment

how to iterate list of string array in java

Iterator<String[]> iterator = myEntries.iterator();
while(itr.hasNext())
    for(String temp : itr.next()) 
        // Don't worry, itr.next() will be created only one time
        // for an each iteration (while)
        System.out.println(temp);
Comment

java loop through list

for (E element : list) {
    . . .
}
Comment

iterate list in java

Iterator itr=list.iterator();
while(itr.hasNext)
{
 system.out.println(itr.next); 
}
Comment

Iterate List> in java

List<List<Int>> arrayList = new ArrayList(); //Java usually infers type parameters in cases as these
for(int i = 0; i < desiredSize; i++){
    List<Int> listAtI = new ArrayList ();
    for(int j = 0; j < rowLength; j++){
        listAtI.set(j, 0);  //sets the element at j to be  0, notice the values are Int not int, this is dues to Javas generics having to work with classes not simple types, the values are (mostly) automatically boxed/unboxed
    }
    arrayList.set(i, listAtI);
}

arrayList.get(5); //returns the list at index 5
arrayList.get(5).get(5) // returns values from column 5 in row 5 
Comment

PREVIOUS NEXT
Code Example
Java :: retrofit 
Java :: java swing get frame size 
Java :: from file to array java 
Java :: how to remove a sting character in java 
Java :: tableau de chaîne de caractère en java 
Java :: java system.out.println not working 
Java :: startswith java 
Java :: How to get a context in a recycler view adapter 
Java :: if else statement 
Java :: settext java 
Java :: findone in spring boot 2.4.1 
Java :: Java Exception handling using Java throw 
Java :: java abstraction 
Java :: on item click listener for recyclerview adapter 
Java :: Java path to file 
Java :: how to draw a triangle in java 
Java :: how to convert 2d to 1d array in java 
Java :: android generate random int 
Java :: int x = 3 & 5 
Java :: float to int in java 
Java :: public static void main(String[] args) in java 
Java :: debug cxf 
Java :: quicksort java 
Java :: how to return arraylist as array in java 
Java :: osmdroid get current zoom level 
Java :: Exception in thread "main" java.lang.NoClassDefFoundError sdkmanager 
Java :: horizontal recyclerview item width half of screen android 
Java :: java binary search 
Java :: difference between getcontext and getapplicationcontext in android 
Java :: current port used by the Java application 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =