Skip to main content

Java 8 Stream map

Java 8 Stream map() is a method which represent a functional programming concept. In a very simple words, the map() is used to transform one object into other by applying a function.
So that’s why Stream.map(Function mapper) takes a function as an argument.

Given below example explain how to use map() method to convert an object to something else.

1- List of String to List of Integer

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TestMap {

	public static void main(String[] args) {
		List<String> stringList = Arrays.asList("1", "2",
				"3", "4", "5");

		List<Integer> numberList = stringList.stream()
				.map(x -> Integer.valueOf(x))
				.collect(Collectors.toList());

		numberList.stream()
				.forEach(x -> System.out.print(x));

	}
}

Output:

1
2
3
4
5

2- List of Strings to Uppercase

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TestMap {

	public static void main(String[] args) {
		List<String> stringList = Arrays.asList("a", "b",
				"c", "d", "e");

		List<String> upperCaseList1 = stringList.stream()
				.map(x -> x.toUpperCase())
				.collect(Collectors.toList());

		// Output: [A, B, C, D, E]
		System.out.println(upperCaseList1);

		// Using method reference technique
		List<String> upperCaseList2 = stringList.stream()
				.map(String::toUpperCase)
				.collect(Collectors.toList());

		// Output: [A, B, C, D, E]
		System.out.println(upperCaseList2);
	}
}

3- List of object to List of String

Get all the employee names from a list of the employees objects.

public class Employee {
	private Long id;
	private String name;
	private String job;
	private double salary;

	public Employee(Long id, String name, String job,
			double salary) {
		super();
		this.id = id;
		this.name = name;
		this.job = job;
		this.salary = salary;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getJob() {
		return job;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TestMap {

	public static void main(String[] args) {
		List<Employee> employeeList = Arrays.asList(
			  new Employee(1L, "Deepak", "Manage", 55000.00d),
				new Employee(2L, "Rahul", "CA", 95000.00d),
				new Employee(3L, "Raj", "Developer", 45000.00d));

		List<String> employeeNames = employeeList.stream()
				.map(x -> x.getName())
				.collect(Collectors.toList());

		System.out.println(employeeNames);
	}
}

Output:

[Deepak, Rahul, Raj]

4- List of objects to List of other objects

public class Trainee {
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Trainee [id=");
		builder.append(id);
		builder.append(", name=");
		builder.append(name);
		builder.append("]");
		return builder.toString();
	}
}
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class TestMap3 {

	public static void main(String[] args) {
		List<Employee> employeeList = Arrays.asList(
				 new Employee(1L, "Deepak", "Manage", 55000.00d),
			        new Employee(2L, "Rahul", "CA", 95000.00d),
			        new Employee(3L, "Raj", "Developer", 45000.00d));
		
		List<Trainee> trainees = employeeList.stream()
				.map(x -> {
					Trainee trainee=new Trainee();
					trainee.setId(x.getId());
					trainee.setName(x.getName());
					return trainee;
				}).collect(Collectors.toList());

		System.out.println(trainees);

	}
}

Output:

[Trainee [id=1, name=Deepak], Trainee [id=2, name=Rahul], Trainee [id=3, name=Raj]]

Leave a Reply

Your email address will not be published. Required fields are marked *

Top