Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using java Stream to get a sum, average, and sort

I am studying java Stream and having hard time solving questions below. The reason why I got stuck is because I have no idea about dealing with Stream<Integer>.

I stumbled upon a solution of "count" by doing list.stream().count(), but other than this, I can't proceed further. Please help me how to deal with these problems and tell me why list.stream().count() works in this situation. So far, I've tried everything I've learned.

public class Question {
    public static void main(String[] args) {
        List<Integer>list = Arrays.asList(5,3,4,1,2);
        System.out.println("sum by using Stream : " + sum);
        System.out.println("count by using Stream: " + count);
        System.out.println("average by using Stream : " + avg);
        System.out.println("sort by using Stream");
    }
}
like image 249
KingJinho Avatar asked Feb 21 '18 12:02

KingJinho


People also ask

How do you find the average of a stream in Java?

IntStream average() returns an OptionalDouble describing the arithmetic mean of elements of this stream, or an empty optional if this stream is empty. Syntax : OptionalDouble average() Where, OptionalDouble is a container object which may or may not contain a double value.

How should we in a stream to calculate sum of elements?

Using IntStream. This method takes a mapper as a parameter, which it uses to do the conversion, then we can call the sum() method to calculate the sum of the stream's elements. In the same fashion, we can use the mapToLong() and mapToDouble() methods to calculate the sums of longs and doubles, respectively.

Which method can be used to compute the average of a stream?

IntStream average() method in Java It gets the average of the elements of the stream.


2 Answers

IntSummaryStatistics stats = Arrays.asList(1,2,3,4)
    .stream()
    .mapToInt(Integer::intValue)
    .summaryStatistics();

stats.getSum();
stats.getCount();
stats.getAverage();

For the sorted, you will have to stream again.

like image 98
Eugene Avatar answered Sep 21 '22 14:09

Eugene


The reason of why list.stream().count() works but list.stream().sum() doesn't is because list.stream() returns a Stream<Integer> and there is a Stream::count method, but there isn't a Stream::sum or Stream::average.

To get the sum and avg first you have to map each integer value in the Stream<Integer> that you get when you do list.stream() to an IntStream. This is the int primitive specialization of Stream. This can be done using the Stream::mapToInt method:

list.stream().mapToInt(Integer::intValue)

Doing this you can use the methods IntStream::sum and IntStream::average:

System.out.println("sum by using Stream : " + list.stream().mapToInt(Integer::intValue).sum());
System.out.println("average by using Stream : " + list.stream().mapToInt(Integer::intValue).average());

Or even better, you can use the IntStream::summaryStatistics to get the sum, count and avg together (also the min and the max value):

System.out.println("sum, count, avg, min and max using Stream : " + list.stream().mapToInt(Integer::intValue).summaryStatistics());

To sort the values you can use the Stream::sorted method:

System.out.println("sort by using Stream: " + list.stream().sorted().collect(Collectors.toList()));

Here is a good post that can help you to understand how to use the Java 8 Stream Api.

like image 33
Juan Carlos Mendoza Avatar answered Sep 20 '22 14:09

Juan Carlos Mendoza