Java 8 sum and total not found
java, java-8, lambda
Solution
You need to change the map function to return a stream of primitives, for example:
double average = users.parallelStream().filter(u -> u.age > 0).mapToDouble(u -> u.age).average().getAsDouble();
^^^^^^^^
The underlying reason is that a `Stream<Double>` (returned by `map`) is not a `DoubleStream` (returned by `mapToDouble`). Only the latter has average and sum methods.
Problem
EDIT: Found the solution here: http://www.dreamsyssoft.com/java-8-lambda-tutorial/map-reduce-tutorial.php I'm following this tutorial: http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html When I get to the part where it's using the sum and average functions, I get the following error: ``` UserAverageTest.java:68: error: cannot find symbol double average = users.parallelStream().filter(u -> u.age > 0).map(u -> u.age).average().getAsDouble(); ^ symbol: method average() location: interface Stream<Double> ``` I get the same error when calling sum. For some reason it appears that it's using the Stream instead of DoubleStream class. I'm using the latest jdk with lambda enabled that is linked in the tutorial. Has anyone hit this issue as well and was able to resolve it? Here is a simple example that reproduces the problem: ``` class User { double age; public User(double age) { this.age = age; } double getAge() { return age; } } public static void main(String[] args) throws Exception { List<User> users = Arrays.asList(new User(10), new User(20), new User(30)); double average = users.parallelStream() .filter(u -> u.age > 0) .map(u -> u.age) .average() .getAsDouble(); } ```