Java Fundamentals

anyMatch, allMatch, & filter

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "anyMatch, allMatch, & filter" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie demonstrates using the anyMatch, allMatch, and filter operations to return the desired data from an array of strings. The filter operation allows for a more precise return in that it will only return items that match the given condition.


Transcript from the "anyMatch, allMatch, & filter" Lesson

>> All right, so we are going to look at some of the common stream operations. What we're gonna do is we're gonna write this code. I'm gonna make this list of veggies this time and then we're gonna look at multiple stream operations, okay? All right, so let's go to our ID.

And we'll make a new package called streams. Inside of streams, let's make a new class. We'll call this StreamOperations. All right, I'm gonna actually make that list global so that I don't have to redefine it for every operation that we choose. So we're gonna say it's a List<String> called veggies.

I need to import the list, okay? And I'm gonna use List.of, who cares that it's not mutable because we're not gonna change the thing anyway. All right? So you can add any veggies you want to yours. I'm gonna put some spinach, some cabbage, Some peas. A little green beans.

Brussels, sprouts. And carrots. All right, so there's our list of veggies. And then we'll need a main method, so that we could call any other methods we want. So the very first operation that we'll look into is the any match operation. If we wanted to see if there are any vegetables that match a certain criteria, we can call the any match method on the stream, all right?

And this any match operation is going to look through the stream to determine if any of the elements happened to match this condition, all right? So, let's make a new method. Public static void, we'll call this any match. I'm gonna say demo so you don't get confused with the actual operation, all right?

And then from main, we're gonna wanna call this. So with any match. And I'm sorry. We need to make this static if we're going to use it in these static methods. Okay. So this one were just gonna say veggies,stream, this will turn it into a stream. .and then we can do any processing we want.

In this case, we want to say any match. Now, any match takes a predicate. This predicate is what is the condition, okay? Predicate also. Predicate is a functional interface, right? So we need to provide an implementation here that will result in a condition that was one of the ones we looked at where it returns a Boolean as the result, right?

So we can specify this with a lambda expression. So we wanna say, okay, do any of the veggies, I don't know, let's say we wanna make sure, I mean, we wanna see if any of the vegetables are two words, like they contain a space in them or something like that.

So we wanna say, okay, does any of the veggies contain a space? How will we write that as a lambda expression? Any takers? What's the first thing we would put?
>> A value like a A single letter.
>> v
>> A single letter. What are we gonna call it?

>> v carrot or v-
>> All right, here we go. Say v for veggie, there you go. And then what?
>> Arrow thing.
>> Arrow.
>> Arrow, all right, perfect. And now we're gonna say, okay, that's the input. And then what do we wanna do with this? We wanna say, does v contain And we'll put a white space, all right?

So that will say, basically, give me Anything that matches this. Now, if we were to run this, we would get, what is this return? Any match, this returns a boolean. So should we print this out? Let's just say boolean any found. Or let's call it any matches, any matches =.

So I can assign that there, right? And then we'll just print that out. So I'm gonna run this. And yep, we got true because it found what? The green beans and the brussel sprouts, right? Questions on any match? So because this is a, any match is a terminal operation, there's no other operations I can do on this.

If I would have put .notice there's no methods here. We're done. It has closed the stream and is done with us, okay? And the reason, if you think about this, why would this one be terminal is because it's not actually collecting those things. It would be much different let say, get the ones that match this condition.

It's not, it's basically doing a true or false, there's no other objects there. It's true or false, right?
>> Then you could build out an f loop or whatever, where if that boolean condition is true, pull out the objects that match.
>> Well, there's other operations you can use, so you wouldn't even use any match.

You would use something else that says, filter this down to those or something like that. All right, let's do another one. How about we'll say public static void allMatch. So the allMatch operation determines if every single element in the stream matches a certain condition. So we can say boolean allMatches = and then we'll use the list and we'll turn it into a stream.

And then we can just call this allMatch operator. The allMatch also takes a predicate. So we would do the same thing with our lambda expression. We would say v-> and then what do we wanna match? Maybe let's see if everything contains the letter s, for example. So I can say v.contains ("s") And then we'll print out all matches.

And don't forget in the main, we have to call that. Okay? If I were to run allMatch, I would get false because let's see if there are any veggies without a s. Yes, cabbage does not have an s, so this is false. Now, I have not looked into this before but I'm pretty sure that once it finds one thing, that would make this all be false, it just kind of stops.

[LAUGH] it doesn't make sense for it to process the rest of the string. Questions on allMatch? All right. This is also a terminal operation. So after it's done the check, it goes ahead and closes the stream. Now, to the question that you asked about, maybe we just wanna pull a few out, so you wouldn't wanna do anyMatch and then lube and stuff, that would be overkill.

There is a filter option that will allow you to filter stuff down. So let's look at that one. This one is really popular. So you definitely wanna remember this one. Oops, filter demo, okay? So in this one, we're gonna say, yes. And we want to use the filter operation.

Filter also takes a predicate, so we're gonna give it something that will result in a boolean condition but what this is saying is to just basically filter this stream down to only the elements that match the condition that I've given it. So we can add a condition here to say, find me all of the vegetables that start with a c.

So we will say v->v.startsWith. And these methods that I'm using contains startsWith. These are not part of stream, they are part of what?
>> String.
>> String.
>> String because the element is a string, very good. All right, so I'm saying find me the things that startWith ("s"), all right?

I mean, c, right? c Okay? So, filter, again, that's gonna filter them down, filter is an intermediate operation, which means, once they give me that narrowed list, I can then do something else with it. So here are all the other operations. Maybe I wanna print it out. Let's just go ahead and print it out.

Print out what we got. So we remember how to do that. We're gonna use the method reference. That out. All right? So if I run this, I only got the vegetables that start with a c.
>> I'm sorry, could you scroll over a little bit on the filter demo, so I can see the full line for the stream?

>> And it's best practice. What I did here is kinda raggedy. So it's best practice after the stream, you would do something like new line filter, new line and have it appear vertical, so that it's easier to read, all right? So after I did this for each, for each is terminal.

So I no longer have the stream anymore. I couldn't do anything else with it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now