Created
October 15, 2017 00:01
-
-
Save jsbonso/a50e485da0029c2c4ca6d9403f825915 to your computer and use it in GitHub Desktop.
Java ForEach Lambda Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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