fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Map;
  10.  
  11. import static java.util.stream.Collectors.groupingBy;
  12.  
  13. /* Name of the class has to be "Main" only if the class is public. */
  14. class Ideone
  15. {
  16.  
  17. public static void main(String[] args) {
  18. List<Employee> listEmployee = new ArrayList<>();
  19. listEmployee.add(new Employee("Ravi", "IT", true));
  20. listEmployee.add(new Employee("Tom", "Sales", false));
  21. listEmployee.add(new Employee("Kanna", "IT", false));
  22.  
  23. Map<String, List<Employee>> employeePerDep = listEmployee.stream().collect(groupingBy(Employee::getDepartement));
  24. System.out.printf("%10s %10s %10s %10s\n", "Departement", "total", "active", "inactive");
  25.  
  26. for (Map.Entry<String, List<Employee>> entry : employeePerDep.entrySet()) {
  27. int total = entry.getValue().size();
  28. long active = entry.getValue().stream().filter(Employee::isActive).count();
  29. System.out.printf("%-10s %10d %10s %10s\n", entry.getKey(), total, active, total - active);
  30. }
  31. }
  32. }
  33.  
  34. class Employee {
  35. private String name;
  36. private String departement;
  37. private boolean active;
  38.  
  39. Employee(String name, String sector, boolean active) {
  40. this.name = name;
  41. this.departement = sector;
  42. this.active = active;
  43. }
  44.  
  45. public String getName() {
  46. return name;
  47. }
  48.  
  49. public void setName(String name) {
  50. this.name = name;
  51. }
  52.  
  53. boolean isActive() {
  54. return active;
  55. }
  56.  
  57. void setActive(boolean active) {
  58. this.active = active;
  59. }
  60.  
  61. String getDepartement() {
  62. return departement;
  63. }
  64.  
  65. void setDepartement(String departement) {
  66. this.departement = departement;
  67. }
  68. }
Success #stdin #stdout 0.09s 35072KB
stdin
Standard input is empty
stdout
Departement      total     active   inactive
Sales               1          0          1
IT                  2          1          1