List.map() vs Stream().map() in Java

kiarash shamaii
3 min readJul 8, 2023

--

what is different between list.map and stream().map.collect() in java?

java

Both List.map() and Stream.map().collect() are used to apply a function to each element of a collection and produce a new collection with the transformed elements. However, there are some key differences between the two approaches.

List.map() is a method of the List interface in Java, which returns a new List with the transformed elements. The map() method applies the given function to each element of the list in order and returns a new list with the transformed elements. For example, the following code uses List.map() to transform a list of integers by squaring each element:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squaredNumbers = numbers.map(x -> x * x);

On the other hand, Stream.map().collect() is a method of the Stream interface in Java, which returns a new collection after applying the function to each element of the stream. The map() method in Stream applies the given function to each element of the stream in order and returns a new stream with the transformed elements. The collect() method is then used to convert the stream into a collection. For example, the following code uses Stream.map().collect() to transform a list of integers by squaring each element:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> squaredNumbers = numbers.stream().map(x -> x * x).collect(Collectors.toList());

One key difference between the two approaches is that List.map() returns a new List object, whereas Stream.map().collect() returns a new collection type that can be specified by the Collectors class. This allows more flexibility in the type of collection that is created.

Another difference is that Stream.map().collect() operates lazily on the elements of the collection, which means that the elements are transformed one by one as they are needed, rather than all at once. This can be more memory-efficient for large collections.

When you use Stream.map().collect() to transform a collection, the transformation is performed lazily. This means that the elements are not transformed all at once, but rather one by one as they are needed. This can be more memory-efficient for large collections because it avoids the need to hold all the elements and their transformed values in memory at the same time.

For example, let’s say you have a collection with a million elements and you want to transform each element by multiplying it by 2 and store the transformed elements in a new list. If you use List.map() to transform the elements, the entire collection will be loaded into memory and transformed all at once. This can be memory-intensive, especially if the collection is very large.

On the other hand, if you use Stream.map().collect(), the transformation is performed lazily and the elements are transformed one by one as they are needed. This means that only one element is transformed and stored in memory at a time, rather than the entire collection. This can be more memory-efficient, especially if the transformation is complex or if the collection is very large.

In addition to being more memory-efficient, lazy evaluation can also be faster in some cases because it allows operations to be performed in parallel on multiple elements. This can improve the performance of operations that are computationally intensive or that involve I/O operations.

Finally, Stream.map().collect() allows for more advanced operations to be performed on the collection, such as filtering, sorting, and grouping, using the other methods available in the Stream interface.

--

--