
The Stream.map() function in Java 8 converts a stream to another form. The java Stream.map() acts as a bridge between two operations. In other words, it is an intermediate operation.
The map() accepts an input stream and returns a result to a stream which is sent the output stream. If we have a type X input stream and use the Stream.map() method, that input stream will be converted to a type Y output stream.
Table of Contents
Convert ArrayList to Uppercase java by using Stream
Suppose we have a list of strings and we need to convert all the strings list to upper case.
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class ConvertListToUpperCase {
public static void main(String[] args) {
List<String> listOfStrings = new ArrayList<>();
listOfStrings.add("java");
listOfStrings.add("c#");
listOfStrings.add("html");
listOfStrings.add("angular js");
List<String> upperCaseListUsingLoop = new ArrayList<>();
// By using loop
for (String str : listOfStrings) {
upperCaseListUsingLoop.add(str.toUpperCase());
}
System.out.println("Using Loop: " + upperCaseListUsingLoop);
// By using stream
List<String> upperCaseListUsingStream = new ArrayList<>();
upperCaseListUsingStream = listOfStrings.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println("Using Stream: " + upperCaseListUsingStream);
}
}
Output: Using Loop: [JAVA, C#, HTML, ANGULAR JS] Using Stream: [JAVA, C#, HTML, ANGULAR JS]
Get values from list object in Java 8
Here is an Employee class in which we have single constructor. We are creating a list of Employee object.
From the list of Employee object we have to find the list of name and collect in the list.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Employee {
private int empId;
private String ename;
private double salary;
public Employee(int empId, String ename, double salary) {
super();
this.empId = empId;
this.ename = ename;
this.salary = salary;
}
public int getEmpId() {
return empId;
}
public String getEname() {
return ename;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", ename=" + ename + ", salary=" + salary + "]";
}
}
public class ListOfObjectToListOfString {
public static void main(String[] args) {
List<Employee> employeeList = Arrays.asList(new Employee(1, "SCOTT", 30000), new Employee(2, "RASHID", 72000),
new Employee(3, "VIRAT", 50000));
List<String> employeeNameList = employeeList.stream().map(emp -> emp.getEname()).collect(Collectors.toList());
System.out.println(employeeNameList);
}
}
Output: [SCOTT, RASHID, VIRAT]
Convert List<Object> into comma separated String
We have a list of Employee objects, and we need to gather all of the names and need to separate by commas.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Employee {
private int empId;
private String ename;
private double salary;
public Employee(int empId, String ename, double salary) {
super();
this.empId = empId;
this.ename = ename;
this.salary = salary;
}
public int getEmpId() {
return empId;
}
public String getEname() {
return ename;
}
public double getSalary() {
return salary;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", ename=" + ename + ", salary=" + salary + "]";
}
}
public class ListOfObjectToCommaSepartedString {
public static void main(String[] args) {
List<Employee> employeeList = Arrays.asList(new Employee(1, "SCOTT", 30000), new Employee(2, "RASHID", 72000),
new Employee(3, "VIRAT", 50000), new Employee(4, "SMITH", 51000));
String commaSeparatedString = employeeList.stream().map(emp -> emp.getEname()).collect(Collectors.joining(","));
System.out.println(commaSeparatedString);
}
}
Output: SCOTT,RASHID,VIRAT,SMITH
Java-stream map to Array
In this example we have a line of strings. We are converting the lines into an array of strings using Stream.map(). This is a simple example of how to convert the whole String to upper case
import java.util.Arrays;
public class StreamMapToArray {
public static void main(String[] args) {
String lineString = "Hi, This is an example Stream Map to Array!";
String[] result = Arrays.stream(lineString.split("\\s+")).map(String::toUpperCase).toArray(String[]::new);
System.out.println(Arrays.toString(result));
}
}
Output: [HI,, THIS, IS, AN, EXAMPLE, STREAM, MAP, TO, ARRAY!]
Convert int[] to Integer[] by using Stream.map()
This example is very useful for interview question. In this example we Convert primitive type array to wrapper type array.
Here we will have an array of type int (int[]) and using Stream.map() we will convert to array of type Integer(Integer[]).
import java.util.Arrays;
public class PrimitiveToWrapper{
public static void main(String[] args) {
int[] intArray = { 10, 20, 30, 40, 50 };
Integer[] integerArray = Arrays.stream(intArray).map(x -> x * 2).boxed().toArray(Integer[]::new);
System.out.println(Arrays.toString(integerArray));
}
}
Java 8 stream map multiple statements
In a single statement, we may use multiple map functions. Here’s an example of a multiple map function in operation.
In this example the first map() function multiplies the value by 2 and the second map() function multiplies the value of the first map() by 3.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class MultipleMapFunction {
public static void main(String[] args) {
List<Integer> valueList = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> result = valueList.stream().map(e -> e * 2).map(e -> e * 3).collect(Collectors.toList());
System.out.println(result);
}
}
Output: [6, 12, 18, 24, 30]
Recommended Articles
- Basic Description of Java 8 Lambda Expressions
- Interview Questions and Answers Java 8
- Features in Java 11