Finding Maximum Age of Employee using Java
Categories: Java 8(JDK1.8) Java Java Examples
Finding Maximum Age of Employee 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 FindingMaximumAgeOfEmployee {
public static void main(String[] args) {
//Finding Maximum Age of Employee 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("Finding Maximum Age of Employee using Java............");
Employee maxAgeObj = list.stream().max((e1,e2)->e1.getAge()-e2.getAge()).orElse(null);
System.out.println("Maximum age Object :- \n"+maxAgeObj);
//or
long maxAge=list.stream().mapToInt(e->e.getAge()).max().getAsInt();
System.out.println("Maximum age :-"+maxAge);
}
}
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+ "]";
}
}