Finding the Count of Male and Female Employees
Categories: Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CountMaleAndFemale {
public static void main(String[] args) {
//Finding the Count of Male and Female Employees
List<Employee> list = new ArrayList<Employee>();
list.add(new Employee("Rajesh", 121.0, "IT","Lucknow",24,"Male"));
list.add(new Employee("Rani", 122.0, "ACT","NOIDA",35,"Female"));
list.add(new Employee("Rahul", 122.0, "ACT","NOIDA",35,"Male"));
list.add(new Employee("Rohit", 123.0, "ACT","Delhi",36,"Male"));
list.add(new Employee("Sonu", 124.0, "OPR","Ambedkar Nagar",37,"Male"));
System.out.println("Count Male & Female............");
Map<String, Long> countMaleAndFemale = list.stream().collect(Collectors.groupingBy(Employee::getGender,Collectors.counting()));
System.out.println(countMaleAndFemale);
}
}
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+ "]";
}
}