EP52 – Most Common Functional Interfaces in Java
Description
In this episode we'll talk about 7 common Functional Interfaces that Java provides to us.
UnaryOperator, BinaryOperator, Supplier, Consumer, Function, Predicate and BiPredicate
Episode Transcript
0:09
Welcome to the coders campus podcast, where you'll learn how to code from one of the best teachers in the industry. Whether you're an absolute beginner or a seasoned pro, the coders campus podcast will teach you what you need to know to master the art of programming.
0:24
And now, your host, Trevor page. All right, ladies and gentlemen, fellow coders. Welcome to Episode 52 of the coders campus podcast. As always a pleasure to be here, again, bringing you the knowledge that you need to succeed as a coder. So on that vein, as per last episode, just want to preface this by saying, if you haven't checked out the boot camp, yet, we have cohorts launching every single month. And if your goal is to get a job as a coder in as little time as possible, within reason, we're not talking about, hey, do this little thing. And in two months, you'll be getting a job with 100,000 notes. So we're a lot more realistic here at coders campus. So yeah, our boot camps are about six months in duration, we have a very high success rate. But yeah, we'll talk more about that at the end of the episode. So if you haven't already checked it out, coderscampus.com/bootcamp if you're interested in launching your career as a coder. Alright, so now into the content. For today's episode, we are continuing our talk on functional interfaces. We mentioned in the last episode that there were some popular functional interfaces that are provided to us by the good folks at Java at I guess Oracle owns it currently. And these functional interfaces that are provided to us are quite useful. So much so that like I had alluded to in the previous episode, we don't tend to create our own functional interfaces, we only tend to leverage the existing ones. So obviously, let's talk about the existing ones that are out there, in no particular order. But these are the popular ones, there are many more than the sort of five that we're going to be talking about today. But the the, the the other ones are sort of variations of these five that we're going to talk about. So once you know and understand these five, the rest of them tend to be
2:27
fairly straightforward to understand. You'll see them and say, Oh, that's a variation of one of the five that Trevor taught me and I get it right. So let's start with the first with the five. Let me see is it 512345667? Sorry, I've been saying five, seven.
2:45
Although anyway, we'll dive into the popular ones. So again, just a recap, in case you Well, if you missed the last episode, go back and listen to it. Because if you don't want to functional interfaces, you're going to be entirely lost with this conversation right now. So highly recommend going back to the previous episode to listen up and learn about functional interfaces. But the first functional interface that we will talk about
3:11
is one called the predicate. Now these names are terrible, in my opinion, I hear the word predicate, and I'm sure maybe some people it clicks and make sense, it still hasn't clicked in my brain yet. But predicate is the name of this functional interface anyway, and it takes one generic type T. Okay, so this is a throwback to generics, hopefully you understand what generics are. Very quick recap of generics, you can think of these single capital letters that are known as generics usually encased in the angle brackets. For example, if you see a list, so give a list of something like a list of strings. The other is usually I say, of, but I you write out the angle brackets, so less than and greater than symbol. And in between those less than greater than symbols, you put the type. So in this case, if you have a list of strings, you would say list, open angle bracket, and you would put string as a data type, and then you close the angle bracket. And then you could instantiate that list with as an array list of strings or, or it could be a list of, you know, any object can be a list of double, it could be a list of user, it could be a list of anything. The point is the of their the word of typically means generic, right? It's a list of something. And that could be anything therefore that anything can be generic. Previously, you would use just the object type. Okay, object would be what you would throw in there, but then you need to cast it all the time, you'd have to cast object to your desired type. And casting can be a little bit wonky at times it can cause issues and it's just a it's just more mess. So Java introduced generics in, I think, version five or something. I've done an episode on generics. So if you don't know what generics are, I would obviously recommend brushing up on that as well. Now
5:00
That's all I will say about generics getting back to these functional interfaces, specifically the predicate functional interface. predicate takes one type T. Okay, so it uses generics a generic type. So it takes a single parameter, okay with this single type,
5:21
and it returns a Boolean.
5:25
So it has one input to its method.
5:29
And it has a return value of Boolean, so it returns a Boolean.
5:35
Okay, so this is like a framework, this is a very generic function, right? That's so you can think of these functional interfaces as as like, sort of templates for functions that are generic that you can just use. Because think of any function that you've written in the past, have you ever written a function that took in a single parameter and then returned to Boolean? I'm sure you have written a function that does that, for example, I've written one called validate user before in some of my projects, right? You want to validate a user. So you take in a user object, and inside of that user object, which has a type, there's a, you know, a username and a password or something. And then you take the username and password and you compare it against your database to see if this user exists in the database. If it does, does the passwords match exactly. If they do, then you return true. This is a valid user, or you return false. This is not a valid user. Right? So that's single
6:31
example of validate user as a as a method of function. That could be classified as a predicate. That's what predicate is all about. That's the generic, you know, functional interface that Java gives to us this generic template that we can leverage
6:49
in a bunch of places. One common place that you would use a predicate is in something called filter. So filter is something that comes with streams, we'll talk about streams, after we've done with this lesson, most likely.
7:04
filter allows us to filter out items from a collection. So if you have a list of users, and let's say in that list of users, you only want to be you only want to consider users that are active or something as an example, you only want to look at active users, active users can be defined as anything, but let's define them as you know, users that have paid their bill, right. So they're not, they haven't failed payments, or something like that, I don't know. So that the users in your system are active, and you only want to look at the active users, how you would do that is you would you would get a collection of all users. This is one way of doing it. You can have a collection of all users and then you can filter out the ones that are not active. So what filter does is it takes a predicate filter is a function that takes a predicate as its input. Okay, this is where in the past episode, I was talking about how the concept of functional interfaces and the introduction of functional interfaces here allows us to treat functions as parameters. Right? This we couldn't do this before. In the world of Java, you couldn't pass a function as a parameter to another function. Right? That's just it couldn't be done. You couldn't you can't do that. Now that became popular in JavaScript. And I'm sure other languages as well, other functional languages. And then Java, like I said, before, copied it shamelessly copied it and said, Hey, look at this great new feature that we have, are we the best? So yeah, this is what's happening here. The filter function takes another function in this case, functional interface as a parameter, and more specifically, it takes the predicate functional interface as a parameter, okay? Because the predicate takes in a type and returns a Boolean, right? So you filter out users that are that are not active, the filter method would take in a predicate, and the predicate would be you pass in a user, as, you know, part of your collection of users that you have, like, say, you have a list of users, you take in the user, that's your input. You type input type is user and the individual users in the collection, the list of users are what is being fed in one at a time, and the output is true or false. So in this case, you would sit you you know, write some code to see is this user active or not. So you would return true if the user is active, you would return false if the user is not active, that's a predicate right and that allows us to use this lambda syntax with the filter function. You know dot filter is a function, the input to the filter is this predicate and remember predicate is a functional interface. Therefore, you can use the lambda
9:56
which call it syntax. So filter can take
10:00
In a function so to speak, you type in the lambda syntax. So filter would, you know, you pass in a user, so user w




