Sitelet https://java2blog.com/java-8-stream-map/

Java 8 Stream Map

Java 8 Stream map

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

<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 to Stream
  • You can also use map method to convert it from Stream to Stream.
    For example: Calling toUppercase() 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.

    Java 8 Stream map

    You will give input as Stream of employee objects and get an output as Stream of String.

    1. Create a class named Employee.java

    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

    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:

    John
    Martin
    Mary
    Steve

    Here is logical representation of above program.
    MapEmployee
    You can use method reference to at line no.17 as below

    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.

    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:

    60

    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.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.