Java 8 Stream API Basic Examples.
Let us understand the few ways where we can use java 8 streams to solve the problems.github link: https://github.com/RamakanthB/Java8Lamda-Streams
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class StreamBasics {
public static void main(String[] args) {
List<Employee> emplist = getAllEmpData();
System.out.println("*********** Print All Employees Details ***********");
emplist.stream().forEach(System.out::println);
System.out.println("*********** Only Name of Employees ***********");
emplist.stream().map(s -> s.getName()).forEach(System.out::println);
System.out.println("*********** Only DEV Dept Employees ***********");
emplist.stream().filter(s -> "DEV".equals(s.getDept())).map(s ->
s.getName()).forEach(System.out::println);
System.out.println("***** Only DEV Dept ans store in new List of Beans*****");
List<Employee> devemplist = emplist.stream().filter(s -> "DEV".equals(s.getDept()))
.collect(Collectors.toList());
devemplist.stream().forEach(System.out::println);
System.out.println("*********** Only STC Project ***********");
long count = emplist.stream().flatMap(s ->
Arrays.stream(s.getProjects())).peek(System.out::println)
.filter(s -> s.equals("STC")).count();
System.out.println("People who are assigned with STC " + count);
System.out.println("*********** Highest salary ***********");
Optional<Employee> higestSal = emplist.stream().reduce((s1, s2) -> s1.getSalary() >
s2.getSalary() ? s1 : s2);
higestSal.ifPresent(System.out::println);
}
public static List<Employee> getAllEmpData() {
List<Employee> emplist = Arrays.asList(
new Employee("Ramakanth", 20000d, "DEV", 27, new String[] { "STC", "TMOB", "GPAYROLL" }),
new Employee("Sanjay", 10000d, "TES", 27, new String[] { "IBM", "ACC" }),
new Employee("Wasim", 17000d, "DEV", 27, new String[] { "STC", "TMOB" }),
new Employee("Umakanth", 8000d, "AID", 25, new String[] { "NXP" }),
new Employee("Pavan", 30000d, "DEV", 27, new String[] { "STC", "CGI" }),
new Employee("Vijay", 10000d, "TES", 27, new String[] { "TMOB", "GPAYROLL" }),
new Employee("Debasis", 17000d, "DEV", 27, new String[] {"GPAYROLL" }),
new Employee("Siva", 8000d, "AID", 27, new String[] { "STC", "TMOB" }),
new Employee("Hema", 40000d, "DEV", 32, new String[] { "STC", "TMOB", "GPAYROLL" }),
new Employee("Deepak", 22000d, "TES", 31, new String[] { "TMOB", "GPAYROLL" }));
return emplist;
}
}
class Employee {
private String name;
private Double salary;
private String Dept;
private Integer age;
private String[] projects;
public String getName() {
return name;
}
public String[] getProjects() {
return projects;
}
public void setProjects(String[] projects) {
this.projects = projects;
}
public void setName(String name) {
this.name = name;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getDept() {
return Dept;
}
@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + ", Dept=" + Dept + ", age=" + age + ", projects="
+ Arrays.toString(projects) + "]";
}
public Employee(String name, Double salary, String dept, Integer age, String[] projects) {
super();
this.name = name;
this.salary = salary;
Dept = dept;
this.age = age;
this.projects = projects;
}
public void setDept(String dept) {
Dept = dept;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
0 comments:
Post a Comment