Java 8 stream map()

map() method in java 8 stream is used to convert an object of one type to an object of another type or to transform elements of a collection.
map() returns a stream which can be converted to an individual object or a collection, such as a list.

Typical applications of map() would be
1. Convert a list of object of one type to a list of objects of another type.
2. Convert a list of string in lower case to a list of upper case strings.
3. Create a list of numbers which are square or cube of numbers in another list etc.

Java doc for map() states,

Returns a stream consisting of the results of applying the given function to the elements of this stream.

map() is invoked for each element of the stream. It is an intermediate operation.

Signature of map() method is

<R> Stream<R> map(Function<? super T, ? extends R> mapper);

where R and T are generic types

map() accepts an argument of type java.util.function.Function as argument.
Function is a functional interface with a single method apply() which takes a single argument and returns a value.

This means that we can supply a Lambda expression to map() which accepts a value and returns another value.

Signature of apply() is

R apply(T t)

If you match the signatures of apply() and map() closely, then the return type of apply() is same as the return type of map().
This means that the type of stream returned by map() is the same as the type of value returned by the lambda expression that is supplied to map().

If this is not clear to you, don’t worry. The examples that follow will clarify the usage of map().
Stream map() to convert list of objects
Let’s say we want to convert list of Subscriber objects to a list of User objects.  Below are these two classes

public class Subscriber {

  private String sName;
  
  // getter and setter methods
}

public class User {

  private String uName;

  // getter and setter methods
}

Below is an example of map() method for this conversion.

List<User> users = subscribers.stream().
     map(s -> {
      User u = new User();
      u.setuName(s.getsName());
      return u;
    }).collect(Collectors.toList());

Lambda expression supplied to map() is creates a new User object and populates its field with the corresponding field of Subscriber class.
Remember that map() will be called for each element of the stream.
map() will return a stream and to convert it to a list, use its collect() method with Collectors.toList().

Stream map() to convert list of objects to list of string
Below is an example to convert a list of objects to a list of string.
For example, we might want the list of names of Subscribers from the list of Subscriber objects.

subscribers.stream().
map(s -> s.getSName()).
collect(Collectors.toList());

Stream map() to convert string to integer
map() method can be used to convert a list of string to a list of integers and vice-versa. Following is an example to convert a list of string to a list of integers.

List<Integer> str = List.of("1","2","3","4");
List<String> nums = str.stream().
                    map(s -> Integer.parseInt(s)).
                    collect(Collectors.toList());

This code can also be written using method reference as

List<Integer> str = List.of("1","2","3","4"); 
List<String> nums = str.stream().
                   map(Integer::parseInt).
                   collect(Collectors.toList());

To convert a list of integers to a list of string values with map(), the code would be

List<String> nums = List.of(1,2,3,4); 
List<Integer> str = nums.stream().
                     map(String::valueOf).
                     collect(Collectors.toList());

Stream map() to convert to upper case
Java 8 stream map() method can be used to convert a list of string to a corresponding list with elements in upper case. Example,

List<Integer> lower = List.of("ab","cd","ef","gh"); 
List<String> upper = lower.stream().
                   map(s -> s.toUpperCase(s)). 
                   collect(Collectors.toList());

With method reference, above code reduces to

lower.stream(). 
map(String::toUpperCase).
collect(Collectors.toList());

That is all on java 8 stream map() method explanation and examples. Hope the article was useful.

0
Liked the article ? Spread the word...