Skip to main content
  1. About
  2. For Teams
Active reading. Expanded. Used more standard formatting. Removed unnecessary formatting.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 110
  • 134

Using older Java versions, including Java 7Java 7, you can use a foreach loop as follows.

List<String> items = new ArrayList<>();
        items.add("A");
        items.add("B");
        items.add("C");
        items.add("D");
        items.add("E");
    
        for(String item : items) {
            System.out.println(item);
        }

FollowingThe following is the very latest way of using a foreachfor each loop in Java 8

Java 8 (loop a List with forEach + lambda expression or method reference).

Lambda

//lambda
    //Output : A,B,C,D,E
    items.forEach(item->System.out.println(item));

 

Method reference

//method reference
    //Output : A,B,C,D,E
    items.forEach(System.out::println);

For more infoinformation, refer this linkto "Java 8 forEach examples".

https://www.mkyong.com/java8/java-8-foreach-examples/

Using older Java versions including Java 7 you can use foreach loop as follows.

List<String> items = new ArrayList<>();
        items.add("A");
        items.add("B");
        items.add("C");
        items.add("D");
        items.add("E");
    
        for(String item : items){
            System.out.println(item);
        }

Following is the very latest way of using foreach loop in Java 8

(loop a List with forEach + lambda expression or method reference)

//lambda
    //Output : A,B,C,D,E
    items.forEach(item->System.out.println(item));

 
//method reference
    //Output : A,B,C,D,E
    items.forEach(System.out::println);

For more info refer this link.

https://www.mkyong.com/java8/java-8-foreach-examples/

Using older Java versions, including Java 7, you can use a foreach loop as follows.

List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");

for(String item : items) {
    System.out.println(item);
}

The following is the very latest way of using a for each loop in Java 8 (loop a List with forEach + lambda expression or method reference).

Lambda

// Output: A,B,C,D,E
items.forEach(item->System.out.println(item));

Method reference

// Output: A,B,C,D,E
items.forEach(System.out::println);

For more information, refer to "Java 8 forEach examples".

Source Link
Du-Lacoste
  • 13k
  • 3
  • 80
  • 58

Using older Java versions including Java 7 you can use foreach loop as follows.

List<String> items = new ArrayList<>();
        items.add("A");
        items.add("B");
        items.add("C");
        items.add("D");
        items.add("E");
    
        for(String item : items){
            System.out.println(item);
        }

Following is the very latest way of using foreach loop in Java 8

(loop a List with forEach + lambda expression or method reference)

//lambda
    //Output : A,B,C,D,E
    items.forEach(item->System.out.println(item));


//method reference
    //Output : A,B,C,D,E
    items.forEach(System.out::println);

For more info refer this link.

https://www.mkyong.com/java8/java-8-foreach-examples/

Morty Proxy This is a proxified and sanitized view of the page, visit original site.