The java.time.Duration class built into Java 8 and later represents a span of time unattached to the timeline on the scale of hour-minutes-seconds. The class offers a plus method to sum two such spans of time.
The java.time classes use immutable objects. So the Duration::plus method returns a new third Duration object as a result rather than altering (mutating) either of the input objects.
The conventional syntax to sum a collection of Duration objects would be the following.
Duration total = Duration.ZERO;
for ( Duration duration : durations )
{
total = total.plus( duration );
}
Can streams be used in place of this for loop?