Posts

Showing posts with the label Iterate Over a List in Java

Iterate Over a List in Java - Six different ways

In this section, we will show 6 different ways to iterate through a List in Java. Using For Loop Using doWhile Using Advanced For Loop Using Iterator Using While Loop Using Java 8 forEach 1. Using For Loop   // create list List < String > languages = new ArrayList< String >(); // add 5 different values to list languages .add( "Java" ); languages .add( "Kotlin" ); languages .add( "Scala" ); languages .add( "Python" ); languages .add( "Go" ); // iterate via "for loop" System . out .println( "==> For Loop Example." ); for ( int i = 0 ; i < languages .size(); i ++) { System . out .println( languages .get( i )); } 2. Using doWhile // create list List < String > languages = new ArrayList< String >(); // add 5 different values to list languages .add( "Java" ); languages .add( "Kotlin" ); languages .add( "Scala" ); languages .add( "Python" ); languages...