Printing the Number of Employees in Each Department using Java
Categories: Java 8(JDK1.8) Java Java Examples
Printing the Number of Employees in Each Department using Java
package r4r.co.in;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class PrintingTheNubmersOfEmployeesInEachDepartment {
public static void main(String[] args) {
// Printing the Number of Employees in Each Department
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("Rajesh", 3121.0, "IT", "Lucknow", 24, "Male"));
list.add(new Employee("Rani", 122.0, "ACT", "NOIDA", 35, "Female"));
list.add(new Employee("Rahul", 1322.0, "ACT", "NOIDA", 35, "Male"));
list.add(new Employee("Rohit", 123.0, "ACT", "Delhi", 36, "Male"));
list.add(new Employee("Sonu", 3124.0, "OPR", "Ambedkar Nagar", 37, "Male"));
System.out.println("Printing the Number of Employees in Each Department...........");
Map<String, Long> map = list.stream()
.collect(Collectors.groupingBy(Employee::getDep, Collectors.counting()));
System.out.println(map);
}
}
class Employee {
String name;
double sal;
String dep;
String city;
int age;
String gender;
public Employee(String name, double sal, String dep, String city, int age, String gender) {
super();
this.name = name;
this.sal = sal;
this.dep = dep;
this.city = city;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public String getDep() {
return dep;
}
public void setDep(String dep) {
this.dep = dep;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Employee [name=" + name + ", sal=" + sal + ", dep=" + dep + ", city=" + city + ", age=" + age
+ ", gender=" + gender + "]";
}
}