Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 sum and total not found

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();
}
like image 644
Rocky Pulley Avatar asked Aug 12 '13 00:08

Rocky Pulley


People also ask

How will you get the sum of all numbers present in a List using Java 8?

We generally iterate through the list when adding integers in a range, but java. util. stream. Stream has a sum() method that when used with filter() gives the required result easily.

How do you sum a count in Java?

So you simply make this: sum=sum+num; for the cycle. For example sum is 0, then you add 5 and it becomes sum=0+5 , then you add 6 and it becomes sum = 5 + 6 and so on.

How do I add two numbers in Java 8?

b) By Using the Sum() Method The sum() method is used to add numbers given in the arguments of the method. The sum() method is in the Integer class of Java which is in the util package. We will use the Integer. sum() function.

What is a flatMap in Java 8?

In Java 8 Streams, the flatMap() method applies operation as a mapper function and provides a stream of element values. It means that in each iteration of each element the map() method creates a separate new stream. By using the flattening mechanism, it merges all streams into a single resultant stream.


1 Answers

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.

like image 192
assylias Avatar answered Oct 04 '22 13:10

assylias