Table of contents
Overview on map java 8 stream
Syntax : Stream map(Function mapper)
Usage : We will use map function in java 8 to convert an element from one form to other .
Parameters : Function ( This function contains the transformation logic )
Returns : It returns a new stream with transformed values .
Example : If you want to double all the numbers in a list you can use map method .
Example to double an integer value
package map;
import java.util.List;
import static java.util.stream.Collectors.*;
public class MapFunction {
public static void main(String[] args) {
List < Integer > list = List.of(1, 3, 4, 7, 10);
List < Integer > doubled = list.stream()
.map(element -> element * 2)
.collect(toList());
System.out.println(doubled);
}
}
O / P: [2, 6, 8, 14, 20]
We can write the transformation logic as a separate lambda expression and pass it to the map function .
package map;
import java.util.List;
import static java.util.stream.Collectors.*;
interface NumberDoubled {
int doubled(int number);
}
public class MapFunction {
public static void main(String[] args) {
List < Integer > list = List.of(1, 3, 4, 7, 10);
// Transformation logic lambda expression
NumberDoubled numberDoubled = (element) -> element * 2;
List < Integer > doubled = list.stream()
.map(element -> numberDoubled.doubled(element))
.collect(toList());
System.out.println(doubled);
}
}
O / P: [2, 6, 8, 14, 20]
Multiple map functions on a list
It is possible to use multiple map functions in Java 8 stream .
Example : Double the integer value in a list
Double the integer value from the above-transformed list .
Here we will be doing the transformation twice .
package map;
import java.util.List;
import static java.util.stream.Collectors.*;
public class MapFunction {
public static void main(String[] args) {
List < Integer > list = List.of(1, 3, 4, 7, 10);
List < Integer > doubled = list.stream()
.map(element -> element * 2)
.map(element -> element * 2)
.collect(toList());
System.out.println(doubled);
}
}
O / P: [4, 12, 16, 28, 40]
Map list of objects to a list of properties
We have a list of Persons . We will map the persons to their names .
package map;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
import java.util.List;
import static java.util.stream.Collectors.*;
public class MapFunction {
public static List < Person > createPeople() {
return List.of(
new Person("Suraj", 30),
new Person("Iqbal", 20),
new Person("Amit", 31),
new Person("Suchit", 73),
new Person("Amar", 11),
new Person("Suraj", 22),
new Person("Bhaskar", 35)
);
}
public static void main(String[] args) {
List < String > names = createPeople().stream()
.map(Person::getName)
.collect(toList());
System.out.println(names);
}
}
O / P: [Suraj, Iqbal, Amit, Suchit, Amar, Suraj, Bhaskar]
Map based on condition
We have a list of Persons . We will print the names if the age is greater than 30 .
This can be done in 2 ways :
1 ) We will filter the Persons based on age and then map it to name .
package map;
import java.util.List;
import static java.util.stream.Collectors.*;
public class MapFunction {
public static List < Person > createPeople() {
return List.of(
new Person("Suraj", 30),
new Person("Iqbal", 20),
new Person("Amit", 31),
new Person("Suchit", 73),
new Person("Amar", 11),
new Person("Suraj", 22),
new Person("Bhaskar", 35)
);
}
public static void main(String[] args) {
List < String > names = createPeople().stream()
.filter(person -> person.getAge() > 30)
.map(Person::getName)
.collect(toList());
System.out.println(names);
}
}
O / P: [Amit, Suchit, Bhaskar]
2 ) We will write a separate lambda expression that takes a person and returns a name if age is greater than 30 . We will pass this lambda to the map function .
package map;
import java.util.List;
import java.util.Objects;
import static java.util.stream.Collectors.*;
interface MapToName {
String mapToName(Person person);
}
public class MapFunction {
public static List < Person > createPeople() {
return List.of(
new Person("Suraj", 30),
new Person("Iqbal", 20),
new Person("Amit", 31),
new Person("Suchit", 73),
new Person("Amar", 11),
new Person("Suraj", 22),
new Person("Bhaskar", 35)
);
}
public static void main(String[] args) {
// Lambda expression to filter person object based on age and return name
MapToName mapped = person -> {
if (person.getAge() > 30) {
return person.getName();
}
return null;
};
List < String > names = createPeople().stream()
.map(person -> mapped.mapToName(person))
.filter(Objects::nonNull).collect(toList());
System.out.println(names);
}
}
O / P: [Amit, Suchit, Bhaskar]
Note* We have made use of the filter in the above example because in a few iterations the mapped lambda expression will return null and that would be displayed in the output . Since I don’t want to display null values hence I have made use of that filter .
Hope you liked this article on the map java 8 stream . If you want to add anything extra to this article please comment below . You can also write to me if you want to learn any specific topic on Java 8 .
2 thoughts on “How to use Map function in Java 8 Stream ?”