Printing Average Age of Male and Female Employees using Java
Categories: Java 8(JDK1.8) Java Java Examples
Printing Average Age of Male and Female Employees 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 PrintingAverageAgeOfMaleAndFemaleEmployees {
public static void main(String[] args) {
// Printing Average Age of Male and Female Employees using Java
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 Average Age of Male and Female Employees...........");
Map<String, Double> map = list.stream()
.collect(Collectors.groupingBy(Employee::getGender, Collectors.averagingDouble(Employee::getAge)));
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 + "]";
}
}