Type Here to Get Search Results !

Streams API Tutorial in Java 8+

0
Employee.java
 package com.example.demo;  
 import java.util.List;  
 public class Employee {  
 private String firstname;  
 private String lastname;  
 private double salary;  
 private List<String> project;  
 @Override  
 public String toString() {  
      return "Employee [firstname=" + firstname + ", lastname=" + lastname + ", salary=" + salary + ", project=" + project  
                + "]";  
 }  
 public Employee(String firstname, String lastname, double salary, List<String> project) {  
      super();  
      this.firstname = firstname;  
      this.lastname = lastname;  
      this.salary = salary;  
      this.project = project;  
 }  
 public String getFirstname() {  
      return firstname;  
 }  
 public void setFirstname(String firstname) {  
      this.firstname = firstname;  
 }  
 public String getLastname() {  
      return lastname;  
 }  
 public void setLastname(String lastname) {  
      this.lastname = lastname;  
 }  
 public double getSalary() {  
      return salary;  
 }  
 public void setSalary(double salary) {  
      this.salary = salary;  
 }  
 public List<String> getProject() {  
      return project;  
 }  
 public void setProject(List<String> project) {  
      this.project = project;  
 }  
 }  
Demoapplication.java
 package com.example.demo;  
 import java.util.ArrayList;  
 import java.util.HashMap;  
 import java.util.List;  
 import java.util.Map;  
 import java.util.stream.Collectors;  
 import java.util.stream.Stream;  
 import org.springframework.boot.SpringApplication;  
 import org.springframework.boot.autoconfigure.SpringBootApplication;  
 import org.springframework.context.annotation.Bean;  
 import org.springframework.web.client.RestTemplate;  
 @SpringBootApplication  
 public class DemoApplication {  
      static List<Employee> ss=new ArrayList<>();   
      static {  
           ss.add(new Employee("nima", "narayan", 65654, List.of("PROJECT 1","PROJECT 2")));  
           ss.add(new Employee("ajay", "singh", 77747, List.of("PROJECT 1","PROJECT 2")));  
           ss.add(new Employee("dipak", "kumar", 65654, List.of("PROJECT 1","PROJECT 2")));  
           ss.add(new Employee("harinrer", "singh", 3000, List.of("PROJECT 1","PROJECT 2")));  
      }  
      public static void main(String[] args) {  
           SpringApplication.run(DemoApplication.class, args);  
      ss.stream().forEach(Employee->System.out.println(Employee));  
      List<Employee> saray=ss.stream().map(employee -> new Employee  
                (employee.getFirstname(),employee.getLastname(),employee.getSalary()*1.10,employee.getProject())).collect(Collectors.toList());  
      System.out.println(saray);  
      List<Employee> filterem=ss.stream().filter(Employee ->Employee.getSalary()>5000).map(employee -> new Employee  
                (employee.getFirstname(),employee.getLastname(),employee.getSalary()*1.10,employee.getProject())).collect(Collectors.toList());  
      System.out.println(filterem);  
      Employee findfirst=ss.stream().filter(Employee ->Employee.getSalary()>5000).map(employee -> new Employee  
                (employee.getFirstname(),employee.getLastname(),employee.getSalary()*1.10,employee.getProject())).findFirst().orElse(null);  
      System.out.println(findfirst);  
           }  
      }  
Tags

Post a Comment

0 Comments

Recent-post