1. Introduction
This tutorial will guide you through creating a Java program that identifies the employees with the highest experience based on their year of joining. This information is essential for recognizing long-serving employees and for planning succession and retention strategies.
Key Points
- Modification of the Employee class to include a yearOfJoining attribute.
- Use of Java's Stream API to calculate experience and identify the most experienced employees.
- Handling potentially multiple employees with the same maximum experience.
2. Program Steps
1. Extend the Employee class to include yearOfJoining.
2. Populate a list of Employee instances.
3. Calculate the experience of each employee and find the maximum experience.
4. Filter and list all employees who have this maximum experience.
5. Display the results.
3. Code Program
// File: Employee.java
public class Employee {
private int id;
private String name;
private int age;
private long salary;
private String gender;
private String deptName;
private String city;
private int yearOfJoining;
public Employee(int id, String name, int age, long salary, String gender, String deptName, String city, int yearOfJoining) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.gender = gender;
this.deptName = deptName;
this.city = city;
this.yearOfJoining = yearOfJoining;
}
public int getExperience() {
return Calendar.getInstance().get(Calendar.YEAR) - yearOfJoining;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", gender='" + gender + '\'' +
", deptName='" + deptName + '\'' +
", city='" + city + '\'' +
", yearOfJoining=" + yearOfJoining +
'}';
}
}
// File: FindHighestExperience.java
import java.util.*;
import java.util.stream.Collectors;
public class FindHighestExperience {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee(1, "Aditi", 30, 100000, "F", "HR", "Mumbai", 1995),
new Employee(2, "Rahul", 25, 130000, "M", "Engineering", "Bangalore", 2000),
new Employee(3, "Vishal", 34, 110000, "M", "Engineering", "Mumbai", 1998),
new Employee(4, "Lakshmi", 28, 150000, "F", "HR", "Bangalore", 1992),
new Employee(5, "Priya", 24, 90000, "F", "Marketing", "Delhi", 2005)
);
int maxExperience = employees.stream()
.mapToInt(Employee::getExperience)
.max()
.orElse(0);
List<Employee> mostExperiencedEmployees = employees.stream()
.filter(e -> e.getExperience() == maxExperience)
.collect(Collectors.toList());
mostExperiencedEmployees.forEach(System.out::println);
}
}
Output:
Employee{id=4, name='Lakshmi', age=28, salary=150000, gender='F', deptName='HR', city='Bangalore', yearOfJoining=1992}
Explanation:
1. The Employee class includes yearOfJoining, which is essential for calculating the experience.
2. Experience is computed as the difference between the current year and the yearOfJoining.
3. The FindHighestExperience main method determines the maximum experience by mapping each employee to their experience and using max.
4. It then filters out all employees who match this maximum experience and collects them into a list.
5. These employees are printed out, showcasing those with the highest tenure in the organization.
Comments
Post a Comment
Leave Comment