How can I get a List from some class properties with Java 8 Stream?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In Java 8, the Stream API introduced a powerful and efficient way to process collections of objects. One common application is extracting a list of specific properties from a collection of objects. This article will explore how to use Java 8 Streams to convert a list of objects into a list of a specific class property.
Understanding Java Streams
Java Streams are sequences of elements that support various operations, possibly transforming these into a different type of stream. Streams enable functional-style operations on collections, providing developers with a more declarative way to work with data.
Key Stream Operations
- Intermediate Operations: Transform a stream into another stream. Common intermediate operations include
filter,map, andsorted. - Terminal Operations: Produce a result or a side-effect. Common terminal operations include
collect,forEach, andreduce.
Example: Extracting a List of Class Properties
Assume you have a class Person with properties name and age. You can extract names from a list of Person objects using Java Streams as follows:
Explanation
- Creating the Stream:
people.stream()initializes a stream from theList<Person>. - Mapping: The
map(Person::getName)intermediate operation calls thegetNamemethod on eachPersonobject in the stream, transforming it into a stream of names (Stream<String>). - Collecting: The
collect(Collectors.toList())terminal operation collects the elements of the stream into aList.
Technical Details
- Lambda Expressions: Streams work seamlessly with lambda expressions and method references (
Person::getName). This makes operations concise and readable. - Type Safety: Generics ensure that the transformations are type-safe. The
mapmethod's transformation maintains the specific type information across operations.
Collecting Other Property Types
You can adapt the example for different property types:
Example: Extracting Ages
Combining Streams
Streams can be further manipulated by chaining multiple operations:
Summary Table
Here is a table summarizing the key operations:
| Operation | Type | Description |
stream() | Creation | Converts a collection to a stream. |
map() | Intermediate | Transforms each element via a provided function (e.g., method call). Maintains type consistency across transformations. |
filter() | Intermediate | Selects elements based on a predicate. |
collect() | Terminal | Collects the elements into a collection (e.g., List). |
Conclusion
Using Java 8 Streams to extract class properties from a collection offers a concise, efficient, and readable approach to data manipulation. By employing functional operations such as map and collect, developers can handle complex data transformations with minimal code and maximum clarity.
Incorporating Streams into your Java applications not only enhances code expressiveness but also takes advantage of parallelism and laziness in stream processing, evident in more performance-optimized and declarative Java programs.

