for each list java code example

Example 1: java loop through arraylist

ArrayList<String> namesList = new ArrayList<String>(Arrays.asList( "alex", "brian", "charles") );
         
for(String name : namesList)
{
    System.out.println(name);
}

Example 2: java foreach list

list.forEach((MyType myObject) -> {/* code */});

Example 3: for each java

int [] intArray = { 10, 20, 30, 40, 50 };
        
for( int value : intArray ) {
   System.out.println( value );
}

Example 4: java for each

//itemset contains variables of type <Data Type>
for(<Data Type> item : itemset) {
  //loop
}

Example 5: java iterate over list

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

Example 6: iterate a list using iterator

Iterator<String> crunchifyIterator = crunchifyList.iterator();
        while (crunchifyIterator.hasNext()) {
            System.out.println(crunchifyIterator.next());
        }