Java Stream map() with examples

 200 total views,  1 views today

Welcome to Java 8 series. In this tutorial, you will learn about java stream map() with examples.

map() is a part of the Stream pipeline and an intermediate operation. It belongs to the Stream interface.

map() operation will transform each element of the stream to another form according to the provided function.

To put simple, map() convert one type of Stream to another type of Stream

e..g Stream<String> to Stream<Integer> or Stream<Lowercase String> to Stream<UpperCase String>

Method description

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

As per official documentation,

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

map() accepts a function interface.A Function interface takes a input as a argument and returns a value.

Since map() is an intermediate operation it will have two characteristics.

  1. map() is lazy loading operation. This means data will only be mapped when a terminal operation is invoked on the stream.
  2. map() operation on a stream will always create a new stream output.

Let us now see a few examples of Java 8 stream map()

Usual way of convert List<String> to List<Integer>

import java.util.ArrayList;
import java.util.List;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");

		List<Integer> listOfIntegers = new ArrayList<Integer>();
		for (String str : list) {
			listOfIntegers.add(Integer.valueOf(str));
		}
		
	}
}

Java stream map() to convert List<String> to List<Integer>

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");
		list.add("4");
		list.add("5");

		List<Integer> listOfIntegers = list.stream().map(item -> 
                                               Integer.valueOf(item))
                                          .collect(Collectors.toList());
         System.out.println(Arrays.toString(listOfIntegers.toArray()));
	}
}

Output

[1, 2, 3, 4, 5]

Java stream map() to convert to UpperCase

import java.util.ArrayList;
import java.util.List;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("apple");
		list.add("orange");
		list.add("banana");
		list.add("papaya");
		list.add("carrot");

		list.stream().map(item -> item.toUpperCase())
                             .forEach(System.out::println);
	}
}

Output

APPLE
ORANGE
BANANA
PAPAYA
CARROT

Java stream map() using Function interface

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

public class StaticReference {
	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("apple");
		list.add("orange");
		list.add("banana");
		list.add("papaya");
		list.add("carrot");

		Function<String, String> function = (String item) ->        item.toUpperCase();
		list.stream()
                    .map(function)
                    .forEach(System.out::println);
	}
}

Conclusion

Java Stream map() operation is mainly used for transforming one form of the stream to another form. map() will always create a new stream and it is lazily evaluated.

References : Official documentation

Thanks for reading. Please a comment and give us a thumbs up! 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.