Viser bare
0:03
S… Speaker 2 (15- The BinaryOperator Interface)
Early in this section we talked about this package,
0:05
S… Speaker 2 (15- The BinaryOperator Interface)
java .util .function.
0:08
S… Speaker 2 (15- The BinaryOperator Interface)
This is where we have all these functional interfaces in java.
0:12
S… Speaker 2 (15- The BinaryOperator Interface)
I told you that the list may appear overwhelming,
0:15
S… Speaker 2 (15- The BinaryOperator Interface)
but the concept behind this list is actually very simple.
0:18
S… Speaker 2 (15- The BinaryOperator Interface)
All these interfaces fall into four categories.
0:21
S… Speaker 2 (15- The BinaryOperator Interface)
We have consumers,
0:22
S… Speaker 1 (15- The BinaryOperator Interface)
suppliers,
0:23
S… Speaker 1 (15- The BinaryOperator Interface)
functions,
0:24
S… Speaker 2 (15- The BinaryOperator Interface)
and predicates.
0:25
S… Speaker 2 (15- The BinaryOperator Interface)
So all the interfaces you saw are specializations of these four
0:29
S… Speaker 2 (15- The BinaryOperator Interface)
types of functional interfaces.
0:31
S… Speaker 2 (15- The BinaryOperator Interface)
Now in this video we're going to talk about a special type of function called binary
0:35
S… Speaker 1 (15- The BinaryOperator Interface)
operator.
0:37
S… Speaker 2 (15- The BinaryOperator Interface)
This addition operator we have here is an example of a binary operator
0:41
S… Speaker 2 (15- The BinaryOperator Interface)
because it has two operands and returns a single
0:45
S… Speaker 1 (15- The BinaryOperator Interface)
result.
0:46
S… Speaker 1 (15- The BinaryOperator Interface)
Now we can represent this concept,
0:48
S… Speaker 1 (15- The BinaryOperator Interface)
this operation,
0:49
S… Speaker 2 (15- The BinaryOperator Interface)
using the binary operator interface.
0:51
S… Speaker 2 (15- The BinaryOperator Interface)
Now as you can see,
0:52
S… Speaker 2 (15- The BinaryOperator Interface)
this interface extends the bifunction interface,
0:56
S… Speaker 2 (15- The BinaryOperator Interface)
so the binary operator interface is a special type of function.
0:59
S… Speaker 2 (15- The BinaryOperator Interface)
It's a function that takes two arguments of type T and returns a
1:04
S… Speaker 1 (15- The BinaryOperator Interface)
result of type T.
1:05
S… Speaker 2 (15- The BinaryOperator Interface)
Now just like the other interfaces,
1:07
S… Speaker 2 (15- The BinaryOperator Interface)
we have primitive specializations of this interface.
1:09
S… Speaker 2 (15- The BinaryOperator Interface)
For example,
1:10
S… Speaker 2 (15- The BinaryOperator Interface)
we have int binary operator that takes two primitive integers
1:14
S… Speaker 2 (15- The BinaryOperator Interface)
and returns an integer.
1:16
S… Speaker 2 (15- The BinaryOperator Interface)
Now let me show you how to use the binary operator interface.
1:19
S… Speaker 1 (15- The BinaryOperator Interface)
So let's declare a binary operator of integer,
1:23
S… Speaker 1 (15- The BinaryOperator Interface)
call it add,
1:26
S… Speaker 2 (15- The BinaryOperator Interface)
and set it to a lambda expression like this.
1:28
S… Speaker 1 (15- The BinaryOperator Interface)
Here we need two parameters,
1:30
S… Speaker 1 (15- The BinaryOperator Interface)
so we should wrap them in parenthesis.
1:31
S… Speaker 2 (15- The BinaryOperator Interface)
We can call them a and b.
1:33
S… Speaker 2 (15- The BinaryOperator Interface)
then add the lambda operator and return a plus b.
1:37
S… Speaker 2 (15- The BinaryOperator Interface)
As simple as that.
1:39
S… Speaker 1 (15- The BinaryOperator Interface)
Now,
1:40
S… Speaker 2 (15- The BinaryOperator Interface)
because the binary operator is a special type of the function interface,
1:43
S… Speaker 1 (15- The BinaryOperator Interface)
here we have the apply method,
1:46
S… Speaker 2 (15- The BinaryOperator Interface)
so we can pass 1 and 2,
1:48
S… Speaker 1 (15- The BinaryOperator Interface)
get the result,
1:49
S… Speaker 2 (15- The BinaryOperator Interface)
and start over here.
1:51
S… Speaker 2 (15- The BinaryOperator Interface)
Now in this example,
1:52
S… Speaker 2 (15- The BinaryOperator Interface)
we are working with primitive integers,
1:54
S… Speaker 2 (15- The BinaryOperator Interface)
so these primitive integers have to be auto -boxed inside
1:58
S… Speaker 2 (15- The BinaryOperator Interface)
instances of the integer class.
2:01
S… Speaker 2 (15- The BinaryOperator Interface)
So if you're dealing with a large number of primitive integers,
2:04
S… Speaker 2 (15- The BinaryOperator Interface)
it is more efficient to use the int binary operator
2:08
S… Speaker 1 (15- The BinaryOperator Interface)
interface.
2:08
S… Speaker 1 (15- The BinaryOperator Interface)
Okay?
2:09
S… Speaker 2 (15- The BinaryOperator Interface)
Now let's make this more interesting.
2:11
S… Speaker 2 (15- The BinaryOperator Interface)
Let's say we want to represent this operation.
2:13
S… Speaker 2 (15- The BinaryOperator Interface)
We have two arguments,
2:15
S… Speaker 2 (15- The BinaryOperator Interface)
like a and b.
2:16
S… Speaker 2 (15- The BinaryOperator Interface)
First we want to add them together,
2:18
S… Speaker 2 (15- The BinaryOperator Interface)
so a plus b,
2:19
S… Speaker 2 (15- The BinaryOperator Interface)
and then we want to get the square of the result.
2:22
S… Speaker 2 (15- The BinaryOperator Interface)
Now to model this,
2:24
S… Speaker 2 (15- The BinaryOperator Interface)
we should have two small functions,
2:26
S… Speaker 2 (15- The BinaryOperator Interface)
each representing one operation,
2:28
S… Speaker 2 (15- The BinaryOperator Interface)
and then compose them.
2:30
S… Speaker 2 (15- The BinaryOperator Interface)
So here's the first function.
2:31
S… Speaker 2 (15- The BinaryOperator Interface)
Now let's create a function to get the square of a number.
2:35
S… Speaker 2 (15- The BinaryOperator Interface)
That is pretty easy.
2:36
S… Speaker 2 (15- The BinaryOperator Interface)
So function of integer and
2:40
S… Speaker 1 (15- The BinaryOperator Interface)
integer.
2:41
S… Speaker 2 (15- The BinaryOperator Interface)
So this function takes an integer and returns another integer.
2:44
S… Speaker 2 (15- The BinaryOperator Interface)
We call it square and set it to a lambda like this.
2:48
S… Speaker 2 (15- The BinaryOperator Interface)
A goes to A times A.
2:50
S… Speaker 2 (15- The BinaryOperator Interface)
We can use function composition to represent this
2:54
S… Speaker 1 (15- The BinaryOperator Interface)
operation.
2:55
S… Speaker 2 (15- The BinaryOperator Interface)
So first we add these numbers and then we
3:00
S… Speaker 1 (15- The BinaryOperator Interface)
square the result.
3:01
S… Speaker 2 (15- The BinaryOperator Interface)
Now if you print the result we should see 9 there
3:07
S… Speaker 2 (15- The BinaryOperator Interface)
you go So this is how we can use the binary operator interface in
3:12
S… Speaker 1 (15- The BinaryOperator Interface)
Java

Denne utskrifta ble laget av AI (automatisk talegjenkjenning). Kan inneholde feil – sjekk mot den opprinnelige lyden for kritisk bruk. AI- praksis

❤️ Elsker du STT.ai? Fortell vennene dine!
Sammendrag
Trykk Summarize for å lage et AI- sammendrag av denne utskrifta.
Summarerer...
Spør AI om dette transkriptet
Spør noe om denne utskrifta – AI- en vil finne relevante avsnitt og svar.