... . For each available ProblemProvider
implementation, the stream contains one Provider<ProblemProvider>
object. Line 24 uses Stream
method collect
and the predefined Collector
defined by Collectors.toList
to get the List
containing all the available implementations. If that List
is empty (line 27) the program displays an appropriate message and terminates.
Using a Service-Provider Interface
If the List
contains any service-provider implementations, lines 36–46 use them to display one math problem at a time to the user. Lines 38–39
ProblemProvider provider =
providersList.get(random.nextInt(providersList.size())).get();
randomly select one Provider<ProblemProvider>
object from the providersList
, then invoke that object’s get
method to obtain ...
Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.