Java Stream Map Examples
In Java 8, stream().map() lets you convert an object to something else.
Stream map(Function mapper) returns a stream consisting of the results of applying the given function to the elements of this stream.
Stream map(Function mapper) is an intermediate operation.
Stream map() Method
<R> Stream<R> map(Function<? super T,? extends R> mapper)
R represents the element type of the new stream.
mapper is a non-interfering, stateless function to apply to each element which produces a stream of new values.
The method returns a new stream of objects of type R.
similarly,we have other method's
Similar methods.
IntStream mapToInt(ToIntFunction<? super T> mapper)
LongStream mapToLong(ToLongFunction<? super T> mapper)
DoubleStream mapToDouble(ToDoubleFunction<? super T> mapper)
Description
The map() is an intermediate operation. It returns a new Stream as return value.
The map() operation takes a Function, which is called for each value in the input stream and produces one result value, which is sent to the output stream.
The mapper function used for transformation is a stateless function (does not store the information of previously processed objects) and returns only a single value.
The map() method is used when we want to convert a Stream of X to Stream of Y.
The mapped stream is closed after its contents have been placed into the new output stream.
map() operation does not flatten the stream as flatMap() operation does.
1). Stream map() function with operation of converting lowercase to uppercase.
In the following example, we have used the Stream Map function to convert letter's to uppercase.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class CovertTUpperCase {
public static void main(String[] args) {
List <String> alpha = Arrays.asList("a", "b", "c", "d");
//Before Java8
List <String> alphaUpper = new ArrayList <>();
for (String s: alpha) {
alphaUpper.add(s.toUpperCase());
}
System.out.println(alpha); //[a, b, c, d]
System.out.println(alphaUpper); //[A, B, C, D]
// In Java 8
List <String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(collect); //[A, B, C, D]
.
List < Integer > num = Arrays.asList(1, 2, 3, 4, 5);
List <Integer> collect1 = num.stream().map(n - <n * 2).collect(Collectors.toList());
System.out.println(collect1); //[2, 4, 6, 8, 10]
}
}
output:
[a, b, c, d]
[A, B, C, D]
[A, B, C, D]
[2, 4, 6, 8, 10]
2). List of objects to List of String
Get all the name of Student's from a list of the Student objects.
public class Student {
private int id;
private String name;
private String dob;
private String standard;
//...
}
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ListOfObject's {
public static void main(String[] args) {
List <Student> students = Arrays.asList(
new Student(1, "Sara", "02-12-2013", "1st")),
new Student(2, "Bala", "04-11-2012", "2nd"),
new Student(3, "Anna", "12-04-2012", "2nd"),
new Student(3, "Joe", "23-09-2012", "2nd")));
//Before Java 8
List < String > result = new ArrayList <>();
for (Student stud: students) {
result.add(stud.getName());
}
System.out.println(result); //[Sara, Bala,Anna, Joe]
//Java 8
List <String> collect = students.stream().map(x - > x.getName()).collect(Collectors.toList());
System.out.println(collect); //[[Sara, Bala,Anna, Joe]
}
}
3). find all distinct salaries among all employees
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Employee {
private int id;
private String name;
private double salary;
Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public double getSalary() {
return salary;
}
}
public class Main {
public static void main(String[] args) {
List <Employee> employeesList = Arrays.asList(
new Employee(1, "Sara", 1500), new Employee(2, "Anna", 2500), new Employee(3, "Bobe", 2500), new Employee(4, "Albert", 1700), new Employee(5, "George", 2300), new Employee(6, "Bala", 1500), new Employee(7, "Joe", 2300));
List <Double> distinctSalaries = employeesList.stream().map(e - > e.getSalary()).distinct().collect(Collectors.toList());
System.out.println(distinctSalaries);
}
}
output:
[1500.0, 2500.0, 1700.0, 2300.0]
4). Stream map() function with operation of mapping string length of string
import java.util.*;
public class GetStringLength {
public static void main(String[] args) {
System.out.println("The length of String's after applying " + "the function is : ");
// Creating a list of Strings
List <String> list = Arrays.asList("Hello", "wORLD", "JavaProgramming", "Computer", "Electonics", "Java");
list.stream().map(str - >str.length()).forEach(System.out::println);
}
}
output:
The length of String's after applying the function is :
5
5
15
8
10
4
In this article, we have seen the Java Stream Map Examples
0 Comments
Post a Comment