KingJinho
KingJinho

Reputation: 763

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");
    }
}

Upvotes: 19

Views: 34029

Answers (4)

Eugene
Eugene

Reputation: 120858

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.

Upvotes: 41

Azzabi Haythem
Azzabi Haythem

Reputation: 2413

You should use mapToInt or mapToDouble

and you can print list by forEach(System.out::println)

List<Integer> list = Arrays.asList(5,3,4,1,2);

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

System.out .println("count by using Stream: " + list.stream().count());

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

System.out.println("sort by using Stream: " );

list.stream().sorted().forEach(System.out::println);

Upvotes: 3

Juan Carlos Mendoza
Juan Carlos Mendoza

Reputation: 5794

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.

Upvotes: 14

Yaroslav
Yaroslav

Reputation: 466

You can do just like this:

List<Integer> list = Arrays.asList(5,3,4,1,2);
        System.out.println("sum by using Stream : " + list.stream().mapToInt(Integer::intValue).sum());
        System.out .println("count by using Stream: " + list.stream().mapToInt(Integer::intValue).count());
        System.out.println("average by using Stream : " + list.stream().mapToInt(Integer::intValue).average());
        System.out.println("sort by using Stream: " + list.stream().sorted().collect(Collectors.toList()));

Upvotes: 4

Related Questions