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|

More Posts related to Java,
- How to install Java 11 on Mac
- Get Client IP address from HTTP Response in Java
- SharePoint Open in the client application document opens in browser
- How to verify if java is installed on the computer and get version detail
- Java - Check if array contains the value
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- How to run Java Unit Test cases with Apache Maven?
- What Java version is used for Minecraft 1.18
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- Convert Instant timestamp into LocalDateTime Java Code Example
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Create simple struts2 project using maven commands
- Java - PatternSyntaxException
- List of Online Java compiler with console
- Minecraft Java Edition
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Java XML-RPC 3.1.x based web service example
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java 8 foreach loop code examples
- Java -Day of the week using Java 8 DayOfWeek Enum
- Java 8 - Convert List to Map Examples
- Java: TimeZone List with GMT/UTC Offset
- list of jars required for hibernate 4.x.x
More Posts:
- Android Activity Main xml stuck loading - Android-Studio
- How to create StackOverflow error in java - Java
- How to fix: You will need Google Chrome to install most apps, extensions and themes. - Chrome
- Sublime Text 3 Convert Case to Upper, Lower, Title or Swap - Sublime-Text
- Java -Day of the week using Java 8 DayOfWeek Enum - Java
- SharePoint list excel import error - Title is a required filed and can't be empty - SharePoint
- How to send SMS on Android Emulator - Android
- Users experience call quality issue, voice distortion, disconnection with Microsoft Teams call and meeting - Teams