Java 8 Streams map() with examples


In Java 8 you can make use of the Streams API if you want to iterate over collections and perform operations over them.

Using stream.map() you can apply a function over the elements of a collection like List and get back a stream that contains the result after applying the function.

✌️The Stream map() function is known as the intermediate operation.

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

Example 1: Add bonus to Salary using Stream.map()

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

/**
 * 
 * Java Stream map() function example
 * 
 * Author: Code2care.org
 * Date: 17-Apr-2022
 * Ver: v1.0
 * 
 */
public class Example {

    public static void main(String[] args) {

        List<Integer> salaries = new ArrayList<>();

        salaries.add(4000);
        salaries.add(10000);
        salaries.add(3000);
        salaries.add(12000);
        salaries.add(6000);
        salaries.add(7000);
        salaries.add(5000);

        System.out.println("Salaries: "+salaries);
        //map as intermediate operation to add bonus to salaries
        List newSalary = salaries.stream().map(n -> n+ ((n*10)/100)).collect(Collectors.toList());
        System.out.println("Salaries + 10% Bonus: "+ newSalary);

    }
}
Output:

Salaries: [4000, 10000, 3000, 12000, 6000, 7000, 5000]
Salaries + 10% Bonus: [4400, 11000, 3300, 13200, 6600, 7700, 5500]

Example 2: Convert to lowercase using Stream.map()

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

/**
 *
 * Java Stream map() function example
 *
 * Author: Code2care.org
 * Date: 17-Apr-2022
 * Ver: v1.0
 *
 */
public class Example {

    public static void main(String[] args) {

        List<String> names = new ArrayList<>();
        names.add("Mike");
        names.add("Alex");
        names.add("Bryan");
        names.add("Tiffany");
        names.add("Andrew");
        
        List lowerCaseNames = names.stream().map(name -> name.toLowerCase()).collect(Collectors.toList());
        System.out.println("Lower case names: " + lowerCaseNames);

    }
}
Output:

Lower case names: [mike, alex, bryan, tiffany, andrew]



Example 3: String ArrayList to Integer ArrayList

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

/**
 *
 * Java Stream map() function example
 *
 * Author: Code2care.org
 * Date: 17-Apr-2022
 * Ver: v1.0
 *
 */
public class Example {

    public static void main(String[] args) {

        List<String> intStringList = new ArrayList<>();
        intStringList.add("1");
        intStringList.add("2");
        intStringList.add("3");
        intStringList.add("4");
        intStringList.add("5");

        ArrayList intArrayList = (ArrayList<Integer>) intStringList.stream().map(strInt -> Integer.parseInt(strInt)).collect(Collectors.toList());
        System.out.println("Integer ArrayList: " + intArrayList);

    }
}
Output:

|Integer ArrayList: [1, 2, 3, 4, 5]HBE|

Java 8 Streams map
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.