How do you sum on a list of objects in a java 8 stream

Travis Cavanaugh

In C# I had this

List<Learner> Learners;
double learnerSum = this.Learners.Sum(learner => learner.Predicted * learner.Alpha);

I want to do the same in java 8. I get as far as

double learnerSum = this.Learners.stream()...

It seems i need a reduce but all the examples are on simple arrays of integers. what am i missing?

nosid

You can create a DoubleStream from a Stream, and use the method sum to get the sum.

double learnerSum = Learners.stream()
    .mapToDouble(l -> l.Predicted * l.Alpha).sum();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find the sum based on some property of objects from List of objects in Java 8 using stream

Java 8 stream - sum of objects

Java 8 How to sum every elements in a list of a list using stream?

How to sum multiple fields of a list of objects using stream in java

Java 8 - How to group and sum on a list of maps using stream collectors

How would you do multiple operations on a java 8 stream?

How do you use java stream api to convert list of objects into a nested map based on information stored inside object?

How do you use java stream api to convert list of objects into a nested map based on information stored inside object?

Java 8 Stream add elements to list and sum

Java 8 Stream: Extract map from list of objects and sum specific key

How do you find the sum of everything inside a list when objects in the list are classes

How do you build an infinite repeating stream from a finite stream in Java 8?

How do you use java-stream to compare two files and find the sum of values in a certain grouping?

java stream : get sum of field in nested list of objects

Java 8: How to stream a list into a list of lists?

How do you store a list of objects in an array?

How do you convert a list of json objects to a list of javascript objects?

How to fill array list by different objects of the same class using stream API in Java 8?

How to move specific objects to the beginning of a list using java 8 Stream API?

how do you find the sum of a sequence in Java

How would you sum the same columns across a list of zoo objects?

java 8 stream for sorting a list of objects based on particular field

Java8 stream reducing list of objects with fluent API

Java 8 Stream find objects in list that match some properties

Count number of objects in a list using Java8 Stream

How do I merge two List of String into one List without duplicate in Java8 stream

Java8: sum values from specific field of the objects in a list

Java 8 Lambdas - How to Sum and Average from a stream

How do I list, map and "print if count>0" with Java 8 / stream API?