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,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Go to Line Number option in Windows Notepad - NotepadPlusPlus
- [JEP 430] Java JDK 21 New Feature - String Templates (Preview) - Java-JDK-21
- 29: Program to move a file in Python - Python
- Handling NullPointerException with Java Predicate - Java
- Calculate Volume of Cone - C-Program
- Android Emulator 5.1.1 not loading on Mac OS X Android Studio - Android-Studio
- How to change the System Settings Sidebar icon size Mac Ventura 13 - MacOS
- How to Sync iPhone with iPad - iOS