How to iterate over an array using Java Streams

Last modified date

Comments: 0

Iterate over an array using Java Streams

String[] myArray = new String[] {"first", "second", "third", "fourth", "fifth"}

Stream.of(myArray).forEach(<<YOUR LOGIC HERE>>);

Printing elements in an array using Java Streams

This stream using a lambda function in the forEach loop to print the element.

String[] myArray = new String[] {"first", "second", "third", "fourth", "fifth"}

Stream.of(myArray).forEach(element -> System.out.println(element));


// This prints:
first
second
third
fourth
fifth

JakTech

Leave a Reply

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

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.