Learn Java 8 streams with an example - map() method

The map() function is a method in the Stream class that represents a functional programming concept. In simple words, the map() is utilized to transform one object into other by applying a function.

Example 1: Stream map() function with operation of converting uppercase to lowercase.

import java.util.*;
import java.util.stream.Collectors;

/*Stream map() function with operation of converting uppercase to lowercase.
*/
public class DriverClass {
public static void main(String[] args) {
List<String> list = Arrays.asList("SAW", "NUN", "JOKER", "REVENANT", "CAST AWAY");
List<String> result = list.stream().map(String::toLowerCase).
                                       collect(Collectors.toList());
System.out.println(result);
}
}

Example 2: Java program to convert a Stream of Strings to a Stream of Integers

import java.util.*;
import java.util.stream.Collectors;

/*Java program to convert a Stream of Strings to a Stream of Integers.
*/
public class DriverClass {
public static void main(String[] args) {
List<String> list = Arrays.asList("1", "3", "6", "22", "12345");
List<Integer> result = list.stream().map(Integer::valueOf).
                              collect(Collectors.toList());
System.out.println(result);
}
}

Example 3: Java Stream map example with objects

import java.util.*;
import java.util.stream.Collectors;

/*Java Stream map example with objects
*/
public class DriverClass {
public static void main(String[] args) {

Emp emp1 = new Emp(1, "sibin", 23232, "developer");
Emp emp2 = new Emp(2, "sabin", 123232, "developer");
Emp emp3 = new Emp(3, "safin", 443232, "developer");
List<Emp> list = new ArrayList<>();
list.add(emp1);
list.add(emp2);
list.add(emp3);
List<Integer> employeeIds = list.stream().map(e -> e.getId()).
                   collect(Collectors.toList());
System.out.println(employeeIds);
}
}
class Emp {
private int id;
private String name;
private long salary;
private String designation;

public Emp() {
}

public Emp(int id, String name, long salary, String designation) {
this.id = id;
this.name = name;
this.salary = salary;
this.designation = designation;
}

public int getId() {
return id;
}

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

public String getName() {
return name;
}

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

public long getSalary() {
return salary;
}

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

public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}
}


More ...

Popular posts from this blog

Spring boot video streaming example-HTML5

DataTable-Pagination example with Spring boot, jQuery and ajax

10 Best Job Posting Sites 2021-2022

Spring boot web project free download:User Registration System

Spring Boot + JPA/Hibernate One to Many mapping example

5 Hardest Puzzle,100% fail answers

Java security AES,SHA256,SHA512,MD5-Spring Boot Project Free Download

Spring Boot-AngularJS-Bootstrap-JPA-CRUD

ReactJS - Bootstrap - Buttons

Spring Boot file upload/download example