Check out a free preview of the full Java Fundamentals course:
The "collect" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie walks through assigning a stream to a defined collection using the collect operation. A demonstration of combining multiple stream operations is also covered in this segment.

Get Unlimited Access Now

Transcript from the "collect" Lesson

[00:00:00]
>> Another one I wanna show you is collect. This one you're going to use all the time if you work with streams. So let's go ahead and make a method, public static void collectDemo. So what collectors you typically use collect on a resulting stream, so you've done some other operation it's an intermediate operation it's giving you like this resulting stream back.

[00:00:29] And then what collect will do is assign that stream to a collection of your choice, all right? So let's take the vegetables and let's filter it down to the ones that end with a S. And then we're going to collect that and put it in its own new thing, okay?

[00:00:58] So we have the veggies we can say veggies.stream, .filter and we said v, v.endsWith s, right? This will give us a resulting stream. So we then can say collect. And inside the collect, you need to specify, what do you want? What kind of, collection do you want? So to do that you'll say Collectors.

[00:01:43] And then you can say to and notice here we got toList, we have toMap, we have toSet, toUnmodifiableList, Map and Set, right? So you can pick what you want. I'm gonna pick a list and now this whole thing is a list, so I would want to store this in a variable so I can say something like list.

[00:02:07]
>> All veggies?
>> Not all veggie all of them in with it is.
>> All veggies.
>> Veggies, veggiesEndingInS. All right, okay? And then from that, now I have an actual collection, cuz before remember I had a stream, it didn't actually modify the original collection. This doesn't modify the original collection either, but it gives me a resulting collection that I now could do whatever I want with.

[00:02:41] So what we've done is loop that's not really a good example. In this case, you would want to maybe send this off to something else, some other method or something like that. If you just want it to print it so that we could see it, we could do that.

[00:03:01] We're going to use for each aren't we? We're gonna say System.out::println.
>> Don't you want veggies ending in s for your print line?
>> Do I need it?
>> For line 66.
>> Do I need it?
>> Do you want to print your veggies?
>> Sorry, you're right.

[00:03:24] I used the wrong collection. Very good. Alright, let me make sure I'm calling this from the main. Alright, collectDemo. Very nice. Let's run it. All right, and look at that peas, green beans, brussels sprouts, and carrots. So let's do one more big one. As you can see, the stream API is really convenient.

[00:03:56] It provides a number of these operations we've only touched on a few of them. There's so many more that we could touch on, but what's really powerful about the stream API is being able to join like a lot of these intermediate operations together. So let's make one more method that'll kind of combine a couple of them so we can see it like the power of it all.

[00:04:21] So we'll say public static void and we'll call this like multiOperationDemo. All right, so I wanna say veggies.stream. All right, that's the first thing gives us the stream. And then I want to sort that stream out, right? That'll give us an intermediate. That's an intermediate operation. So that'll give us that resulting stream.

[00:04:57] Now I want to say alright, sorted and then I want you to filter. I give it some predicate. Maybe I want to get all of the ones that start with a c. So I can say v.startsWith c. This also intermediate, so it'll also return a resulting stream back to me.

[00:05:21] From that, let's say I want to use map. And I wanna say go ahead and turn all of these things to uppercase. So we'll say String::upperCase to uppercase, okay? Map is also intermediate. So I get another resulting stream back. I can say, you know what? Map it again.

[00:05:46] So I can use these multiple times with different things. So I can do something like, I wanna add a word in front of each one, okay? So this one will get a little fun. We can say, all right, take the v. And what I want you to do is say v.transform.

[00:06:13] Ooh, I know, right? And then in transform, this takes another functional interface. So now I'm doing like a lambda inside of a lambda. Gasp, all right? So now I can say, alright, I called it v. Let me just say w, right? W, arrow, and then I want to say put yummy in front.

[00:06:40] Yummy, yeah, with a space. And then add the w back, right? Now I have, still that was intermediate as well, right? Now I'm gonna go ahead and print it. I'm getting a little silly now. So, alright, so we'll say System.out::println. All right, so now you can see how, you could chain these together to do a bunch of different processing on an individual collection.

[00:07:21]
>> Doing it this way almost reminds me of like writing a SQL query.
>> Does it?
>> Well, a little bit just in terms of going by table, by table, by table, joining stuff together.
>> Then what do I do with that data? Then what do I do with that data?

[00:07:33] All right. So notice the output here is yummy CABBAGE in caps, and then yummy CARROTS in caps. So streams are, I don't know, they're super fun to work with for me. These are another form of like functional programming within Java. So relatively new, been around for a while, but still relatively new compared to the age of the language.