Skip to content

Instantly share code, notes, and snippets.

@yohhoy
Last active August 29, 2015 14:08
Show Gist options
  • Save yohhoy/85909aec0b95f4e41e94 to your computer and use it in GitHub Desktop.
Save yohhoy/85909aec0b95f4e41e94 to your computer and use it in GitHub Desktop.
import java.util.stream.IntStream;
public class StreamCollectFailure {
public static void main(String[] args) {
int[] points = { 80, 95, 70, 60 };
double ave = IntStream.of(points).average().orElse(0.0);
double var = IntStream.of(points).collect(
() -> new Double(0.0),
(r,n) -> { r += Math.pow(n - ave, 2); },
(r,s) -> { r += s; });
// This implementation doesn't work (as we'd expect),
// because Double is immutable and of autoboxing/unboxing spec.
System.out.println("ave="+ave);
System.out.println("var="+var);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment