•
The Iterator only allows traversing
over the elements of a
Collection individually. The SplitIterator on
the other hand
allows traversing over the elements of a Collection individually
as well as in bulk.
257.
•
•
•
CHAPTER
09
Miscellaneous
What are the advantages of CompletableFuture class over
the Future Interface?
Answer:
Prior to Java 8, Java had the Callable class and the Future
interface. These could be used to return the result of an
asynchronous computation. However,
the Future interface had
several issues. The CompletableFuture overcomes all these
issues as follows:
A Future cannot be completed manually. So, if a task is
hung, there is no way of completing the task manually.
CompletableFuture on
the other hand has a method
called complete() that allows completing a task manually.
Future has a method called get() that returns the result of
computation. However, this method blocks until the result
becomes available. So,
if you want to perform any
computation on the result of the Future, you need to wait for
the result to be available. A CompletableFuture on the
other hand allows attaching
callbacks to the result of a
computation. These callbacks execute once the result is
available, so there is no need to wait.
The Future interface does not have any exception handling
mechanism. The CompletableFuture on the other hand
has a method called exceptionally() that allows running
alternate code in case an exception occurs.