Skip to content

Instantly share code, notes, and snippets.

@jsbonso
Created October 15, 2017 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsbonso/a50e485da0029c2c4ca6d9403f825915 to your computer and use it in GitHub Desktop.
Save jsbonso/a50e485da0029c2c4ca6d9403f825915 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