Question: In Fig. 17.24, we implemented a stream pipeline that rolled a die 60,000,000 times using values produced by SecureRandom method ints. Package java.time contains types

In Fig. 17.24, we implemented a stream pipeline that rolled a die 60,000,000 times using values produced by SecureRandom method ints. Package java.time contains types Instant and Duration that you can use to capture the time before and after evaluating the stream pipeline, then calculate the difference between the Instants to determine the total time. Use Instant’s static method now to get the current time. To determine the difference between two Instants, use class Duration’s static method between, which returns a Duration object containing the time difference. Duration provides methods like toMillis to return a duration in milliseconds. Use these timing techniques to time the original stream pipeline’s operation, then do so again using class Random from package java.util, rather than SecureRandom.

Fig. 17.24

L // Fig. 17.24: RandomIntStream.java 2 // Rolling a die 60,000,000 times with streams 3 import

L // Fig. 17.24: Random IntStream.java 2 // Rolling a die 60,000,000 times with streams 3 import java. security.SecureRandom; 4 import java.util.function. Function; import java.util.stream.Collectors; 5 6 7 8 9 10 [I 12 13 14 15 16 17 18 19 20 } public class RandomIntStream { public static void main(String[] args) { SecureRandom random= new SecureRandom(); FAN3456 1 2 } Face Frequency 9992993 10000363 10002272 10003810 10000321 10000241 // roll a die 60,000,000 times and summarize the results System.out.printf("% -6s %s %n", "Face", "Frequency"); random.ints (60_000_000, 1, 7) .boxed () .collect(Collectors.groupingBy (Function.identity (), Collectors.counting (())) .forEach ((face, frequency) > System.out.printf("% -6d%d %n", face, frequency));

Step by Step Solution

3.50 Rating (163 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To accomplish the task described we need to measure the time it takes to execute the stream pipeline that rolls a die using both SecureRandom and Rand... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java How To Program Late Objects Questions!