Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Do streams involve upfront calculation in Java?
package net.betaengine;
import java.util.stream.IntStream;
public class StreamCost {
private boolean isPrime(int n) {
System.out.println("Checking if " + n + " is a prime.");
return IntStream.range(2, n).allMatch(x -> n % x != 0);
}
private void run() {
IntStream stream = IntStream.range(1000, 10001).filter(this::isPrime);
System.out.println("Finding second prime...");
stream.skip(1).findFirst().ifPresent(n -> System.out.println("Second prime: " + n));
}
public static void main(String[] args) {
new StreamCost().run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment