How to use Java 8 Stream Map Examples with a List or Array


Converting or transforming a List and Array Objects in Java is a common task when programming. In the tutorial, We show how to do the task with lots of Java examples code by 2 approaches:

  • Using Traditional Solution with basic Looping
  • Using a powerful API – Java 8 Stream Map

Now let’s do details with examples!

Related posts:
Java 8
Java 8 Streams

Java Transform a List with Traditional Solution by Looping Examples

Before Java 8, for mapping a List Objects, we use the basic approach with looping technical solution.

Looping Example – Java Transform an Integer List

– Example: How to double value for each element in a List?

Looping Example – Java Transform a String List

– Example: How to uppercase string value for each element in a String List?

Looping Example – Transform a Custom Object List

Create a Customer class:

– Example: How to get a string list of fullname of Customer List?

Java 8 Stream Map Examples to Convert or Transform a List

With Java 8, We have a powerful Stream API map() method that help us transform or convert each element of stream with more readable code:

-> It returns a stream consisting of the results of applying the given function to the elements of this stream.

Now we can convert or transform a List with following way:

Stream Map Example with Integer List

– Example: Transform a Java number list to a double value number list by using stream map.

– Code:

In above code, we use collect() method to transform stream elements into another container such as a List.

Stream Map Example with String List

– Example: Convert a Java string list to an uppercase string list by Stream Map.

– Code:

Stream Map Example with Custom Object List

– Example: Transform an Java object list to another string list.

– Code:

Parallel & Sequential Stream Map Examples

Stream provides a parallel API processing parallel(), so We can combine it with map() method to leverage the multiple cores on your computer for performance processing data.

– In above code, we use forEach() method to perform an action System.out::println for each stream element.

To switch from concurrently to sequential processing, We can use the sequential() API of Stream:

There are a difference order of items in the output between concurrently processing and sequential processing with forEach() method: result of the sequential processing remains the same order of original list while the parallel processing does NOT.

Java 8 Stream Map combines with Filter and Reduce

Java Stream Map with Filter

We can combines Stream Map with Filter mechanics.


– Code:

See more about Stream Filter at post: link

Java Stream Map with Reduce

Question: How to sum all number of the final stream after mapping?
We can do the calculation by using reduce() function of Java Stream:

– Code:

32 is a result of reduce funtion by sum all all numbers in final stream after mapping: (5*2 + 1*2 + 3*2 + 7*2).

Java 8 Stream Map Examples with Array

To apply Stream Map in Array with Java 8, We do 2 steps:

  • Create Stream from Array Objects.
  • Apply Stream Mapping for Array Objects as the same way we had done with above List Objects.

Example Code:


We had done how to use Java 8 Stream Map with Examples:

  • How to transform Java Array or List with basic looping
  • Apply Java 8 Stream Map to Integer, String, Custom Object List
  • Combine map() method with filter() and reduce() functions of Java 8 Stream<>
  • Apply Stream Map to Java Array
  • Explore how to use parallel & sequential streams with map() method

Thanks for reading! See you later!

By grokonez | April 5, 2019.

Last updated on February 6, 2020.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *