Skip to content

Instantly share code, notes, and snippets.

@shabbirdwd53
Created April 9, 2022 07:14
Show Gist options
  • Save shabbirdwd53/ef990d5da4cb0242e63330a227bca1c1 to your computer and use it in GitHub Desktop.
Save shabbirdwd53/ef990d5da4cb0242e63330a227bca1c1 to your computer and use it in GitHub Desktop.
Stream API Tutorial
package com.dailycodebuffer.streamapi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@SpringBootApplication
public class StreamApiTutorialApplication {
static List<Employee> employees = new ArrayList<>();
static {
employees.add(
new Employee("Shabbir", "Dawoodi", 5000.0, List.of("Project 1","Project 2"))
);
employees.add(
new Employee("Nikhil", "Gupta", 6000.0, List.of("Project 1","Project 3"))
);
employees.add(
new Employee("Shivam", "Kumar", 5500.0, List.of("Project 3","Project 4"))
);
}
public static void main(String[] args) {
//SpringApplication.run(StreamApiTutorialApplication.class, args);
//foreach
employees.stream()
.forEach(employee -> System.out.println(employee));
//map
//collect
Set<Employee> increasedSal =
employees.stream()
.map(employee -> new Employee(
employee.getFirstName(),
employee.getLastName(),
employee.getSalary() * 1.10,
employee.getProjects()
))
.collect(Collectors.toSet());
System.out.println(increasedSal);
//filter
//findFirst
List<Employee> filterEmployee =
employees
.stream()
.filter(employee -> employee.getSalary() > 5000.0)
.map(employee -> new Employee(
employee.getFirstName(),
employee.getLastName(),
employee.getSalary() * 1.10,
employee.getProjects()
))
.collect(Collectors.toList());
System.out.println(filterEmployee);
Employee firstEmployee =
employees
.stream()
.filter(employee -> employee.getSalary() > 7000.0)
.map(employee -> new Employee(
employee.getFirstName(),
employee.getLastName(),
employee.getSalary() * 1.10,
employee.getProjects()
))
.findFirst()
.orElse(null);
System.out.println(firstEmployee);
//flatMap
String projects =
employees
.stream()
.map(employee -> employee.getProjects())
.flatMap(strings -> strings.stream())
.collect(Collectors.joining(","));
System.out.println(projects);
//short Circuit operations
List<Employee> shortCircuit =
employees
.stream()
.skip(1)
.limit(1)
.collect(Collectors.toList());
System.out.println(shortCircuit);
//Finite Data
Stream.generate(Math::random)
.limit(5)
.forEach(value -> System.out.println(value));
//sorting
List<Employee> sortedEmployees =
employees
.stream()
.sorted((o1, o2) -> o1.getFirstName()
.compareToIgnoreCase(o2.getFirstName()))
.collect(Collectors.toList());
System.out.println(sortedEmployees);
//min or max
employees
.stream()
.max(Comparator.comparing(Employee::getSalary))
.orElseThrow(NoSuchElementException::new);
//reduce
Double totalSal =
employees
.stream()
.map(employee -> employee.getSalary())
.reduce(0.0,Double::sum);
System.out.println(totalSal);
}
}
public class Employee {
private String firstName;
private String lastName;
private Double salary;
private List<String> projects;
@Override
public String toString() {
return "Employee{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", salary=" + salary +
", projects=" + projects +
'}';
}
public Employee(String firstName, String lastName, Double salary, List<String> projects) {
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
this.projects = projects;
}
public Employee() {
}
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> getProjects() {
return projects;
}
public void setProjects(List<String> projects) {
this.projects = projects;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment