Tuesday, 6 October 2020

How Use Flat Map with Stream in Java

Map is Function that takes an argument type T and Map to another R type Object which is stream of  T type Object for example if we pass name  to map which is String type to then map return stream of String Stream<String> map work with no problem  if initial passing object not collection type .

So if we pass collection suppose List to map then map function convert it Stream of List Stream<List<T>> after this map function will not able to map a List to R type output Object  

 Using flatMap  Stream method

 You can fix above problem using flatMap .
 
 flatMap Function takes on argument type T which is Stream<List<E>> then map to new created stream of type R which is Stream<E> 

flatMap extract List<E> from Stream<List<E>> then prepare new stream for each object type E present in List<E> to type Stream<E>

FlatMap Java Application 

FlatMapApplication.java

package in.jk.java8.flatmap;

import java.util.ArrayList;
import java.util.List;


import in.jk.java8.SoftwareProject;

public class FlatMapApplication {

public static void main(String[] args) {

System.out.println("Stream API diffrence beetwen map and flatMap ");

List<Employee> empList = Employee.getEmployeeList();
System.out.println("Find Name with map ");
empList.stream()
                       .map(employee -> employee.getName())
                       .forEach(System.out::println);
System.out.println(" \n Problem with map if maping properties retrun collection \n ");

empList.stream()
               .map(employee -> employee.getSoftwareProject())
               .map(List::stream)
               .forEach(System.out::println);
System.out.println(" \n Solution with map if maping properties retrun collection \n ");
 
empList.stream()
               .map(employee -> employee.getSoftwareProject())
               .flatMap(List::stream)
      .forEach(System.out::println);

}

}

Employee .java

class Employee {

private int empId;
private String name;
private String company;
private int salary;
private List<SoftwareProject> softwareProject;

@Override
public String toString() {
return "Employee [empId=" + empId + ", name=" + name + ", company=" + company + ", salary=" + salary
+ ", softwareProject=" + softwareProject + "]";
}

public static List<Employee> getEmployeeList() {

SoftwareProject softwareProject = new SoftwareProject();
softwareProject.setProjectId(1);
softwareProject.setProjectName("E-Filing");
softwareProject.setDomain("Jurisdictions and Law");
softwareProject.setWrritenIn("Java");

SoftwareProject project2 = new SoftwareProject();
project2.setProjectId(2);
project2.setProjectName("SLX India");
project2.setDomain("Employeement");
project2.setWrritenIn("Java");

SoftwareProject softwareProject3 = new SoftwareProject();
softwareProject3.setProjectId(3);
softwareProject3.setProjectName("Equaze");
softwareProject3.setDomain("Education and Management");
softwareProject3.setWrritenIn("Java");

SoftwareProject softwareProject4 = new SoftwareProject();
softwareProject4.setProjectId(4);
softwareProject4.setProjectName("Grains Procurement System");
softwareProject4.setDomain("Food ");
softwareProject4.setWrritenIn("Java");


List<SoftwareProject> projectList = new ArrayList<>();
projectList.add(softwareProject);
projectList.add(project2);
projectList.add(softwareProject3);
projectList.add(softwareProject4);

List<Employee> empList = new ArrayList<Employee>();

Employee employee = new Employee();
employee.setEmpId(1);
employee.setName("J K");
employee.setCompany("Google");
employee.setSalary(1000);
employee.setSoftwareProject(projectList);

Employee employee1 = new Employee();
employee1.setEmpId(2);
employee1.setName("J K");
employee1.setCompany("Google");
employee1.setSalary(2000);
employee1.setSoftwareProject(projectList);

Employee employee2 = new Employee();
employee2.setEmpId(3);
employee2.setName("J K");
employee2.setCompany("Google");
employee2.setSalary(3000);
employee2.setSoftwareProject(projectList);

Employee employee6 = new Employee();
employee6.setEmpId(6);
employee6.setName("JK");
employee6.setCompany("APAR");
employee6.setSalary(7000);
employee6.setSoftwareProject(projectList);

Employee employee7 = new Employee();
employee7.setEmpId(7);
employee7.setName("JK");
employee7.setCompany("JET BRAINS");
employee7.setSalary(8000);
employee7.setSoftwareProject(projectList);

empList.add(employee);
empList.add(employee1);
empList.add(employee2);
empList.add(employee6);
empList.add(employee7);
                return empList;

}

}

SoftwareProject .java

package in.jk.java8;

public class SoftwareProject {
private int projectId;
private String ProjectName;
private String wrritenIn;
private String domain;
public int getProjectId() {
return projectId;
}
public void setProjectId(int projectId) {
this.projectId = projectId;
}
public String getProjectName() {
return ProjectName;
}
public void setProjectName(String projectName) {
ProjectName = projectName;
}
public String getWrritenIn() {
return wrritenIn;
}
public void setWrritenIn(String wrritenIn) {
this.wrritenIn = wrritenIn;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
@Override
public String toString() {
return "SofwareProject [projectId=" + projectId + ", ProjectName=" + ProjectName + ", wrritenIn=" + wrritenIn
+ ", domain=" + domain + "]";
}
}


Output in console ...

 
 Stream API diffrence beetwen map and flatMap 
 
Find Name with map 
J K
J K
J K
JK
JK
 
 Problem with map if maping properties retrun collection 
 
java.util.stream.ReferencePipeline$Head@776ec8df
java.util.stream.ReferencePipeline$Head@4eec7777
java.util.stream.ReferencePipeline$Head@3b07d329
java.util.stream.ReferencePipeline$Head@41629346
java.util.stream.ReferencePipeline$Head@404b9385
 
 Solution with map if maping properties retrun collection 
  
SofwareProject [projectId=1, ProjectName=E-Filing, wrritenIn=Java, domain=Jurisdictions and Law]
SofwareProject [projectId=2, ProjectName=SLX India, wrritenIn=Java, domain=Employeement]
SofwareProject [projectId=3, ProjectName=Equaze, wrritenIn=Java, domain=Education and Management]
SofwareProject [projectId=4, ProjectName=Grains Procurement System, wrritenIn=Java, domain=Food ]
SofwareProject [projectId=1, ProjectName=E-Filing, wrritenIn=Java, domain=Jurisdictions and Law]
SofwareProject [projectId=2, ProjectName=SLX India, wrritenIn=Java, domain=Employeement]
SofwareProject [projectId=3, ProjectName=Equaze, wrritenIn=Java, domain=Education and Management]
SofwareProject [projectId=4, ProjectName=Grains Procurement System, wrritenIn=Java, domain=Food ]
SofwareProject [projectId=1, ProjectName=E-Filing, wrritenIn=Java, domain=Jurisdictions and Law]
SofwareProject [projectId=2, ProjectName=SLX India, wrritenIn=Java, domain=Employeement]
SofwareProject [projectId=3, ProjectName=Equaze, wrritenIn=Java, domain=Education and Management]
SofwareProject [projectId=4, ProjectName=Grains Procurement System, wrritenIn=Java, domain=Food ]
SofwareProject [projectId=1, ProjectName=E-Filing, wrritenIn=Java, domain=Jurisdictions and Law]
SofwareProject [projectId=2, ProjectName=SLX India, wrritenIn=Java, domain=Employeement]
SofwareProject [projectId=3, ProjectName=Equaze, wrritenIn=Java, domain=Education and Management]
SofwareProject [projectId=4, ProjectName=Grains Procurement System, wrritenIn=Java, domain=Food ]
SofwareProject [projectId=1, ProjectName=E-Filing, wrritenIn=Java, domain=Jurisdictions and Law]
SofwareProject [projectId=2, ProjectName=SLX India, wrritenIn=Java, domain=Employeement]
SofwareProject [projectId=3, ProjectName=Equaze, wrritenIn=Java, domain=Education and Management]
SofwareProject [projectId=4, ProjectName=Grains Procurement System, wrritenIn=Java, domain=Food ]



No comments:

Post a Comment