Home core java java stream map method with 5 examples

java stream map method with 5 examples

In this guide we are going to see different examples of Java Stream map method, Java Streams introduced in Java 8.

Stream.map() method is a Stream intermediate operation, an intermediate operation transform a stream into another stream. Java Stream map method produces a new stream after applying a function to each element of the original stream. The new stream could be of different type.

1. Stream interface map method signature

<R> Stream<R> map(Function<? super T,? extends R> mapper)

R is the element type of the new stream. Stream map() Returns a stream consisting of the results of applying the given function to the elements of this stream.

2. How and When do we use Java Stream map method in java ?

We use Stream map method when ever we want to process elements of source stream and create new stream out of it with same type or different type of elements, the Stream map method can be used. Following examples demonstrates how to use Stream.map() in java. Following image illustrates java 8 stream.map() with example.

java stream map method in java 8

2.1. Stream map method Example 1

Following example demonstrates how to increase each Employee salary by 10000.

- Advertisement -
public class Employee {
	
	private long id;
	private String name;
	private double salary;
	
	public Employee(long id, String name, double salary) {
		this.id = id;
		this.name = name;
		this.salary = salary;
	}
       // getters setters and toString()
}
public class StreamMapIncreaseSalaryDemo {

   final static double INCREMENT_SALARY = 10000;

	public static void main(String[] args) {
        
		List<Employee> employees = Arrays.asList(
				new Employee(1, "Gerhard", 90000),
				new Employee(2, "Peter", 100000),
				new Employee(3, "Andriya", 60000),
				new Employee(4, "Ashja", 80000)
				);
		
		List<Employee> newEployees = employees.stream().map(e -> 
		{
			 e.setSalary(e.getSalary()+INCREMENT_SALARY);
		     return e;
		 }).collect(Collectors.toList());
		
		System.out.println(newEployees);
	}
}

Output :

[
 Employee [id=1, name=Gerhard, salary=100000.0],
 Employee [id=2, name=Peter, salary=110000.0],
 Employee [id=3, name=Andriya, salary=70000.0],
 Employee [id=4, name=Ashja, salary=90000.0]
]

2.2. Stream map method Example 2

Following example demonstrates how to change each Employee name to upper case using map() of Stream.

public class StreamMapToUppercaseDemo {

	public static void main(String[] args) {

		List<Employee> employees = Arrays.asList(
				new Employee(1, "Gerhard", 90000),
				new Employee(2, "Peter", 100000),
				new Employee(3, "Andriya", 60000),
				new Employee(4, "Ashja", 80000)
				);
		
		List<Employee> newEmployees = employees.stream().map(e -> 
		{
			 e.setName(e.getName().toUpperCase());
		     return e;
		 }).collect(Collectors.toList());
		
		System.out.println(newEmployees);
	}
}

Output :

[
 Employee [id=1, name=GERHARD, salary=90000.0], 
 Employee [id=2, name=PETER, salary=100000.0], 
 Employee [id=3, name=ANDRIYA, salary=60000.0], 
 Employee [id=4, name=ASHJA, salary=80000.0]
]

2.3. Stream map method Example 3

Following example shows you how to convert list of dates from one format to other format using map() of Stream .

public class StreamMapConvertDatesExample {
	public static void main(String[] args) {

		List<String> scheduleDates = Arrays.asList(
				 "19-07-2018",
				 "01-02-2016",
				 "05-11-2019",
				 "11-09-1992",
				 "14-02-2020"
				);
		
		List<String> formattedScheduleDates = scheduleDates.stream().map(e -> 
		{
			DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy");
			DateTimeFormatter targetFormat = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
			String date = targetFormat.format(format.parse(e));
		     return date;
		 }).collect(Collectors.toList());
		
		System.out.println(formattedScheduleDates);
	}
}

Output :

[
 19-Jul-2018,
 01-Feb-2016,
 05-Nov-2019,
 11-Sep-1992,
 14-Feb-2020
]

2.4. Stream map method Example 4

Following example shows you how to convert a list to set, and in new set each value represents number of occurrences end of it using map() of Stream .

public class StreamMapConvertListToSetExample {
	public static void main(String[] args) {

		List<String> names = Arrays.asList(
				 "Peter",
				 "Martin",
				 "John",
				 "Peter",
				 "Vijay",
				 "John",
				 "Peter"
				);
		
		// Convert to set by appending occurance count at end of name 
		Set<String> listToSet = names.stream().map(e -> 
		{
		     return e+" ("+Collections.frequency(names, e)+")";
		 }).collect(Collectors.toSet());
		
		System.out.println(listToSet);
	}
}

Output :

[
 Peter (3),
 Martin (1),
 Vijay (1),
 John (2)
]

2.5. map() with filter() Example

If we want to filter the elements of stream based on condition we can use Stream.filter(). Below example demonstrates how to list out the employees whose salary greater than 80000.

public class StreamMapWithFilterDemo {

	public static void main(String[] args) {

		List<Employee> employees = Arrays.asList(
				new Employee(1, "Gerhard", 90000),
				new Employee(2, "Peter", 100000),
				new Employee(3, "Andriya", 60000),
				new Employee(4, "Ashja", 80000)
				);
		
		List<Employee> newEmployees = employees.stream()
				.filter(e -> e.getSalary() > 80000)
				.map(e -> e)
				.collect(Collectors.toList());
		
		System.out.println(newEmployees);
	}
}

Output :

[
 Employee [id=1, name=Gerhard, salary=90000.0],
 Employee [id=2, name=Peter, salary=100000.0]
]

3. Conclusion

In this article we shown how to use Stream interface map() method with examples.

References

  1. Stream Java document

- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles