Collecting is very similar to reducing, you take a list of items and process all the values down to a single value. The main difference is that collecting is more flexible with the types you're allowed to return. Learn how the Java Collect function allows you to reduce a list of items down to a single value.
- [Instructor] The final built-in list processing function … that we're going to discuss in this course is called collect. … So far, we've only been using this function … to transform our streams back into lists, … but it can actually do quite a bit more than that. … Java's collect function is actually quite similar to reduce, … except it's more flexible. … Notice that because reduce requires us … to pass a binary operator function, … the return value of reduce is restricted to being … the same type as the elements of the stream … that it's operating on. … And this means that for example, … if we're reducing a list of strings, … the return value of reduce has to be a string, … or if we're reducing a list of integers, … the return value of reduce has to be an integer. … With collect, however, there are no such restrictions. … So as we've seen the basic syntax of collect is like this. … We call .collect on a stream and pass it something … called a collector. … Now, unlike the other special types that we've seen so far, …
Skill Level Advanced
Java Design Patterns: Structuralwith Bethan Palmer1h 38m Intermediate
1. Introduction to Functional Programming
2. First-Class Functions
3. Working with Streams in Java
4. Advanced Concepts
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.