Last active
August 29, 2015 14:08
-
-
Save yohhoy/85909aec0b95f4e41e94 to your computer and use it in GitHub Desktop.
This file contains 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.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