Created
April 8, 2016 14:07
-
-
Save oxbowlakes/948569bda133ad3c0b9b54c5ed2557fc to your computer and use it in GitHub Desktop.
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
import java.util.stream.*; | |
import java.util.function.*; | |
class FMTS { | |
public static void main(String[] args) { | |
IntStream intStream1 = IntStream.of(1, 2); | |
IntStream intStream2 = IntStream.of(1, 2); | |
//FlatMap | |
IntStream infinite1 = intStream1.flatMap( i -> IntStream.iterate(i, IntUnaryOperator.identity()) ); | |
System.out.println(infinite1.findFirst()); //will not terminate | |
//Map + flatten | |
IntStream infinite2 = intStream2.map(i -> IntStream.iterate(i, IntUnaryOperator.identity())).reduce(Stream.empty(), Stream::concat); | |
System.out.println(infinite2.findFirst()); //prints Optional[1] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment