Java 8 amount and amount not found

EDIT: Found solution here: http://www.dreamsyssoft.com/java-8-lambda-tutorial/map-reduce-tutorial.php

I follow this guide:

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/Lambda-QuickStart/index.html

When I get to the part where it uses the middle and middle 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 the sum. For some reason, it seems like it is using Stream instead of the DoubleStream class. I am using the latest jdk with lambda enabled, which is linked in the tutorial.

Has anyone hit this problem and were able to solve 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(); } 
+6
source share
1 answer

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 main reason is that Stream<Double> (returned by map ) is not a DoubleStream (returned by mapToDouble ). Only the latter has average and total methods.

+19
source

Source: https://habr.com/ru/post/951521/


All Articles