Prikazovanje samo
0:03
S… Speaker 2 (11- Transforming a Completable Future)
Sometimes we need to transform the result of an asynchronous task.
0:06
S… Speaker 1 (11- Transforming a Completable Future)
For example,
0:07
S… Speaker 2 (11- Transforming a Completable Future)
our weather service may return a temperature in a complex data structure,
0:11
S… Speaker 2 (11- Transforming a Completable Future)
but perhaps we're only interested in the temperature as a numeric value.
0:15
S… Speaker 2 (11- Transforming a Completable Future)
So we want to map or transform the result into a different type.
0:19
S… Speaker 2 (11- Transforming a Completable Future)
Or as another example,
0:20
S… Speaker 2 (11- Transforming a Completable Future)
imagine the weather service returns the temperature in Celsius,
0:24
S… Speaker 2 (11- Transforming a Completable Future)
but we want to print it in Fahrenheit.
0:25
S… Speaker 2 (11- Transforming a Completable Future)
So let me show you how to do that.
0:27
S… Speaker 1 (11- Transforming a Completable Future)
First,
0:28
S… Speaker 2 (11- Transforming a Completable Future)
I'm going to create a completable future using the supply
0:32
S… Speaker 2 (11- Transforming a Completable Future)
async method.
0:34
S… Speaker 2 (11- Transforming a Completable Future)
Here we pass a supplier that returns the temperature in Celsius,
0:38
S… Speaker 2 (11- Transforming a Completable Future)
let's say 20 degrees.
0:40
S… Speaker 1 (11- Transforming a Completable Future)
Now we get a completable future and store it over here.
0:43
S… Speaker 2 (11- Transforming a Completable Future)
This future object has a method called
0:47
S… Speaker 1 (11- Transforming a Completable Future)
then apply.
0:49
S… Speaker 2 (11- Transforming a Completable Future)
So as I told you before,
0:51
S… Speaker 2 (11- Transforming a Completable Future)
whenever you see then that means execute this piece of code after
0:55
S… Speaker 2 (11- Transforming a Completable Future)
this task or this future is complete.
0:58
S… Speaker 2 (11- Transforming a Completable Future)
So with the apply method we can convert or map the result to a different
1:02
S… Speaker 2 (11- Transforming a Completable Future)
type We also have then apply async which
1:06
S… Speaker 2 (11- Transforming a Completable Future)
will execute this code asynchronously So it will submit it as a new
1:10
S… Speaker 2 (11- Transforming a Completable Future)
task to the underlying executor.
1:13
S… Speaker 2 (11- Transforming a Completable Future)
Okay, so let's call then apply here
1:17
S… Speaker 2 (11- Transforming a Completable Future)
We should pass a function that takes an integer and maps it
1:21
S… Speaker 2 (11- Transforming a Completable Future)
to a different type.
1:21
S… Speaker 2 (11- Transforming a Completable Future)
So we get a result in Celsius
1:25
S… Speaker 2 (11- Transforming a Completable Future)
And we can convert it to Fahrenheit using this formula.
1:28
S… Speaker 2 (11- Transforming a Completable Future)
Celsius times 1 .8 plus
1:33
S… Speaker 2 (11- Transforming a Completable Future)
32.
1:34
S… Speaker 1 (11- Transforming a Completable Future)
Now,
1:35
S… Speaker 2 (11- Transforming a Completable Future)
just like the exceptionally method that you learned about in the last video,
1:38
S… Speaker 2 (11- Transforming a Completable Future)
this method returns a new completable future.
1:42
S… Speaker 2 (11- Transforming a Completable Future)
So if we call the get method on this completable future,
1:45
S… Speaker 2 (11- Transforming a Completable Future)
we'll get the result in Fahrenheit.
1:47
S… Speaker 2 (11- Transforming a Completable Future)
Let me show you.
1:48
S… Speaker 2 (11- Transforming a Completable Future)
So we call get and store the result over here.
1:52
S… Speaker 2 (11- Transforming a Completable Future)
Now to make our code cleaner,
1:55
S… Speaker 2 (11- Transforming a Completable Future)
it's nicer to put each method on a new line.
1:58
S… Speaker 1 (11- Transforming a Completable Future)
That is better.
1:59
S… Speaker 2 (11- Transforming a Completable Future)
Now let's wrap this inside a try catch block.
2:03
S… Speaker 1 (11- Transforming a Completable Future)
So we get the result in Fahrenheit and then print
2:07
S… Speaker 1 (11- Transforming a Completable Future)
it. So that
2:12
S… Speaker 2 (11- Transforming a Completable Future)
is 68 degrees.
2:13
S… Speaker 1 (11- Transforming a Completable Future)
Beautiful.
2:13
S… Speaker 2 (11- Transforming a Completable Future)
Now we can make this code better.
2:16
S… Speaker 1 (11- Transforming a Completable Future)
Instead of passing this formula as a lambda,
2:19
S… Speaker 2 (11- Transforming a Completable Future)
we can encapsulate this inside a method.
2:22
S… Speaker 2 (11- Transforming a Completable Future)
So I'm going to add a static method in this class.
2:27
S… Speaker 2 (11- Transforming a Completable Future)
public static integer to Fahrenheit,
2:30
S… Speaker 1 (11- Transforming a Completable Future)
a bit hard to spell,
2:33
S… Speaker 2 (11- Transforming a Completable Future)
we give it a parameter called Celsius,
2:35
S… Speaker 2 (11- Transforming a Completable Future)
and here we return Celsius times
2:39
S… Speaker 2 (11- Transforming a Completable Future)
1 .8 plus 32.
2:42
S… Speaker 2 (11- Transforming a Completable Future)
Now here we have a compilation error,
2:44
S… Speaker 2 (11- Transforming a Completable Future)
because the result of this expression is a double,
2:47
S… Speaker 2 (11- Transforming a Completable Future)
but we're expecting an integer.
2:48
S… Speaker 2 (11- Transforming a Completable Future)
We can solve this by casting this to an integer,
2:52
S… Speaker 1 (11- Transforming a Completable Future)
the problem is gone.
2:53
S… Speaker 1 (11- Transforming a Completable Future)
Now,
2:54
S… Speaker 1 (11- Transforming a Completable Future)
we can replace this lambda with a method reference.
2:57
S… Speaker 1 (11- Transforming a Completable Future)
So,
2:58
S… Speaker 2 (11- Transforming a Completable Future)
The name of our class is completable futures demo we
3:02
S… Speaker 2 (11- Transforming a Completable Future)
type completable futures demo colon colon to
3:06
S… Speaker 1 (11- Transforming a Completable Future)
Fahrenheit that is better now here
3:10
S… Speaker 2 (11- Transforming a Completable Future)
we don't have to call the get method earlier I showed you how to run code
3:14
S… Speaker 2 (11- Transforming a Completable Future)
upon completion of a task we talked about two methods then run
3:19
S… Speaker 2 (11- Transforming a Completable Future)
for executing a task and Then accept for
3:23
S… Speaker 2 (11- Transforming a Completable Future)
executing a task,
3:24
S… Speaker 2 (11- Transforming a Completable Future)
but also passing the result to that task
3:27
S… Speaker 2 (11- Transforming a Completable Future)
So I'm going to simplify this code.
3:29
S… Speaker 2 (11- Transforming a Completable Future)
Let's grab what we have inside this try block
3:33
S… Speaker 1 (11- Transforming a Completable Future)
Cut it.
3:34
S… Speaker 1 (11- Transforming a Completable Future)
We don't need the try block anymore So here
3:39
S… Speaker 2 (11- Transforming a Completable Future)
we create a completable future to call our remote API Then
3:43
S… Speaker 2 (11- Transforming a Completable Future)
we transform the result to Fahrenheit Now
3:47
S… Speaker 2 (11- Transforming a Completable Future)
instead of calling the get method we call then accept Because
3:52
S… Speaker 2 (11- Transforming a Completable Future)
we want to get the result the result here is going to be temperature in Fahrenheit.
3:55
S… Speaker 1 (11- Transforming a Completable Future)
So F goes to
3:59
S… Speaker 1 (11- Transforming a Completable Future)
print F.
4:00
S… Speaker 1 (11- Transforming a Completable Future)
Now we don't need the print statement anymore and
4:04
S… Speaker 2 (11- Transforming a Completable Future)
we don't need this variable either.
4:06
S… Speaker 1 (11- Transforming a Completable Future)
So we build a completable
4:10
S… Speaker 2 (11- Transforming a Completable Future)
future then we transform it and finally we accept or
4:14
S… Speaker 2 (11- Transforming a Completable Future)
consume the result.
4:16
S… Speaker 1 (11- Transforming a Completable Future)
Let's run our program.
4:17
S… Speaker 1 (11- Transforming a Completable Future)
There you go.
4:20
S… Speaker 2 (11- Transforming a Completable Future)
So here's the temperature in Fahrenheit.
4:21
S… Speaker 1 (11- Transforming a Completable Future)
So this is the benefit of completable futures.
4:25
S… Speaker 2 (11- Transforming a Completable Future)
With this completable futures we can build complex asynchronous operations
4:29
S… Speaker 2 (11- Transforming a Completable Future)
in a declarative way.

Ta prepis je ustvaril AI (avtomatsko prepoznavanje govora). Lahko vsebuje napake – preverite proti originalnemu zvoku za kritično uporabo. Politika AI

❤️ Ljubezen STT.ai?
Povzetek
Kliknite Povzetek, da ustvarite povzetek AI tega transkripta.
Povzetek...
Vprašaj AI o tem transkriptu
Vprašajte karkoli o tem prepisu – AI bo našel ustrezne oddelke in odgovor.