261.
•
•
•
returns the salary value which is a double is used. So, this
causes a compilation error. In order to fix this error, Line 1 needs
to be modified as follows:
employees.sort(Comparator.comparingDouble(employee–>
employee.getSalary()));
Now, the Comparator.comparingDouble() method is used.
This method accepts as
parameter a ToDoubleFunction
interface that compares based on a Double field.
Explain the differences between the
CompletableFuture.runAsync and the
CompletableFuture.supplyAsync methods.
Answer:
Both the runAsync and the supplyAsync methods can be
used to run code asynchronously. However, there are some
differences between the two as follows:
The runAsync method
does not return a result, so it returns
a CompletableFuture
. The supplyAsync method
on the other hand returns a value from the thread. So, it
returns a CompletableFuture.
The runAsync method accepts as parameter a Runnable
instance. The supplyAsync method on the other hand
accepts as parameter a Supplier instance.
The runAsync method executes the code in the Runnable
implementation in a separate thread. The supplyAsync
method on the other hand executes the code in the
Supplier in a separate Thread and returns the value
produced by the Supplier.