Table of Contents
In this post, we will see about Java 8 Stream map function.
stream‘s map method takes single element from input stream and produces single element to output stream. Type of Single input Stream and Output stream can differ.
Java 8 Stream Map function
1 2 3 |
<R> Stream<R> map(Function<? super T,? extends R>mapper) |
Map function returns a stream consisting of the results of applying the given function to the elements of this stream.
Function is a functional interface available in Java 8 java.util.function.Function accepts one input and produce a result.
Few important points about Map
- Map is intermediate operation and returns stream as a result.
- Map operation takes Function as parameter and this function is called on each element of the stream.
- Map() method is used to convert
Stream
toStream
- You can also use map method to convert it from
Stream
toStream
.
For example: CallingtoUppercase()
method in Map method which will again return Stream.
Let’s understand with the help of a simple example.
Java 8 Stream Map example
Let’s say you have a list of employees, now you want to convert it to list of employee names, so you will map each employee object to employee name.
You will give input as Stream of employee objects and get an output as Stream of String.
1. Create a class named Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package org.arpit.java2blog.map; public class Employee { String name; int age; public Employee(String name, int age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } |
2. Create main class named Java8StreamMapMain.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package org.arpit.java2blog.map; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Java8StreamMapMain { public static void main(String args[]) { List<Employee> listOfEmployees = createListOfEmployees(); // Using map function to convert Stream<Employee> to Stream<Stream> List<String> listOfEmployeeNames=listOfEmployees.stream() .map(e -> e.getName()) .collect(Collectors.toList()); listOfEmployeeNames.forEach(System.out::println); } public static List<Employee> createListOfEmployees() { List<Employee> listOfEmployees=new ArrayList<>(); Employee emp1= new Employee("John",20); Employee emp2= new Employee("Martin",20); Employee emp3= new Employee("Mary",20); Employee emp4= new Employee("Steve",20); listOfEmployees.add(emp1); listOfEmployees.add(emp2); listOfEmployees.add(emp3); listOfEmployees.add(emp4); return listOfEmployees; } } |
When you run above program, you will get below output:
Martin
Mary
Steve
Here is logical representation of above program.
You can use method reference to at line no.17 as below
1 2 3 4 5 |
List<String> listOfEmployeeNames=listOfEmployees.stream() .map(Employee::getName) .collect(Collectors.toList()); |
Another example
Let’s say you have list of integers and you want to find sum of double of even numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog.map; import java.util.Arrays; import java.util.List; public class Java8StreamMapNumber { public static void main(String[] args) { List<Integer> listOfNumbers=Arrays.asList(1,2,3,4,5,6,7,8,9,10); int sum= listOfNumbers.stream() .filter(n -> n%2==0) .map(n -> n*2) .reduce(0, Integer::sum); System.out.println(sum); } } |
When you run above program, you will get below output:
If you notice, we have used map function to double of input number in above example.
That’s all about Java 8 Stream map example.