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 another by applying a function.

Example 1: Stream map() function with the 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 List of String to List 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 program to convert List of Employee to List of Integers(Employee Id)

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

/*Java Stream map example
*/
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

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete