package com.example.demo.java_8.Excercise;
private int empId;
private String empName;
private int deptId;
private String status = "active";
private int salary;
public Employee(int empId, String empName, int deptId, String status, int salary) {
super();
this.empId = empId;
this.empName = empName;
this.deptId = deptId;
this.status = status;
this.salary = salary;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + ", deptId=" + deptId + ", status=" + status
+ ", salary=" + salary + "]";
}
}
package com.example.demo.java_8.Excercise;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Java8Progress {
public static void main(String[] args) {
List<Employee> empList = new ArrayList<>();
empList.add(new Employee(101, "Siva", 101, "active", 2000));
empList.add(new Employee(102, "Reddy", 102, "active", 5000));
empList.add(new Employee(103, "Raju", 103, "inactive", 6000));
empList.add(new Employee(104, "Shivam", 104, "inactive", 4000));
empList.add(new Employee(105, "bob", 105, "active", 3500));
empList.add(new Employee(106, "Alice", 106, "inactive", 3500));
empList.add(new Employee(107, "Srinu", 107, "active", 3500));
Map<Integer, List<Employee>> empListBasedOnDept = empList.stream()
.collect(Collectors.groupingBy(Employee::getDeptId, Collectors.toList()));
empListBasedOnDept.entrySet().forEach(entry -> {
System.out.println(entry.getKey() + "----" + entry.getValue());
});
Map<Integer, Long> empListBasedOnDeptCount = empList.stream()
.collect(Collectors.groupingBy(Employee::getDeptId, Collectors.counting()));
empListBasedOnDeptCount.entrySet().forEach(entry ->{
System.out.println(entry.getKey()+ "---"+ entry.getValue());
});
}
}
-----------------------
OUTPUT :-
1----[Employee [empId=101, empName=Siva, deptId=1, status=active, salary=2000]]
2----[Employee [empId=102, empName=Reddy, deptId=2, status=active, salary=5000], Employee [empId=103, empName=Raju, deptId=2, status=inactive, salary=6000]]
3----[Employee [empId=104, empName=Shivam, deptId=3, status=inactive, salary=4000], Employee [empId=105, empName=bob, deptId=3, status=active, salary=3500]]
4----[Employee [empId=106, empName=Alice, deptId=4, status=inactive, salary=3500], Employee [empId=107, empName=Srinu, deptId=4, status=active, salary=3500]]
1---1
2---2
3---2
4---2
---------------------------------------------------------------------------------------------------------
1. What is functional Interface ?
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods.