Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tamboer/644a8f30dd9c3faa851891d03c39fa42 to your computer and use it in GitHub Desktop.
Save tamboer/644a8f30dd9c3faa851891d03c39fa42 to your computer and use it in GitHub Desktop.
Java ForEach Lambda Example
/**
* Example of Java's Stream API - ForEach
* @author jonbonso
* @param args
*/
public static void main(String... args) {
System.out.println("Iterate using the traditional for loop...");
int[] numArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i=0; i<numArray.length; i++) {
System.out.print(numArray[i] + " ");
}
System.out.println("\n\nIterate using Stream ForEach... ");
Stream<Integer> numStream = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Uses Stream's forEach to iterate the given collection
// notably, it is using the lambda expression ( -> )
numStream.forEach(
num -> System.out.print(num + " ")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment