Java stream question, mapToInt and average method

There are two different types: a Stream<Integer> and an IntStream.

Java's generics can't have methods that only apply on some generics. For example, it couldn't have Stream<Integer>.average() and not also have Stream<PersonName>.average(), even though the average person name doesn't make sense.

Therefore, Stream has a mapToInt method that converts it into an IntStream, which then provides the average() method.


IntStream provides average() method, so to use it you need to convert Stream<Integer> to IntStream by using mapToInt method.