Java Reference
In-Depth Information
System
.
out
.
printf
(
"Max: %d, Min: %d, Ave: %f, Sum: %d"
,
trackLengthStats
.
getMax
(),
trackLengthStats
.
getMin
(),
trackLengthStats
.
getAverage
(),
trackLengthStats
.
getSum
());
}
Example 4-4
prints out a summary of track length information to the console. Instead of cal-
culating that information ourselves, we map each track to its length, using the primitive spe-
cialized
mapToInt
method. Because this method returns an
IntStream
, we can call
sum-
maryStatistics
, which calculates statistics such as the minimum, maximum, average, and
sum values on the
IntStream
.
These values are available on all the specialized streams, such as
DoubleStream
and
LongStream
. It's also possible to calculate the individual summary statistics if you don't
need all of them through the
min
,
max
,
average
, and
sum
methods, which are all also avail-
able on all three primitive specialized
Stream
variants.
Overload Resolution
It's possible in Java to
overload
methods, so you have multiple methods with the same name
but different signatures. This approach poses a problem for parameter-type inference because
it means that there are several types that could be inferred. In these situations
javac
will pick
the
most specific
type for you. For example, the method call in
Example 4-5
,
when choosing
Example 4-5. A method that could be dispatched to one of two methods
overloadedMethod
(
"abc"
);
Example 4-6. Two methods that are overloaded
private
private
void
void
overloadedMethod
(
Object o
) {
System
.
out
.
print
(
"Object"
);
}
private
private
void
void
overloadedMethod
(
String s
) {
System
.
out
.
print
(
"String"
);
}