Employee Management with TreeSet
Create an application that:
- Stores employee records in a TreeSet.
- Uses Comparable to sort by Employee ID.
- Uses Comparator to display employees by:
- Salary
- Name
- Experience
- Provides menu-driven operations:
- Add Employee
- Remove Employee
- Search Employee
- Display by ID
- Display by Salary
- Display by Name
- Exit
This project demonstrates practical use of TreeSet, Comparable, and Comparator and is an excellent exercise for Java Full Stack interview preparation.
This is a Java Full Stack interview-level project and is frequently asked in interviews because it combines OOP, Collections, Comparable, Comparator, and Menu-Driven Programming.
Step-1 Employee Class (Comparable)
package com.silan;
public class Employee implements Comparable<Employee>{
private int id;
private String name;
private double salary;
private int experience;
public Employee(int id, String name, double salary, int experience) {
this.id = id;
this.name = name;
this.salary = salary;
this.experience = experience;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public int getExperience() {
return experience;
}
@Override
public int compareTo(Employee e) {
}
@Override
public String toString() {
return String.format("%-6d %-15s %-10.2f %-5d",
id,name,salary,experience);
}
}
Step-2 Salary Comparator
package com.silan;
import java.util.Comparator;
public class SalaryComparator implements Comparator<Employee>{
@Override
public int compare(Employee e1, Employee e2) {
int result = Double.compare(e1.getSalary(), e2.getSalary());
if(result==0)
return Integer.compare(e1.getId(), e2.getId());
return result;
}
}
Step-3 Name Comparator
package com.silan;
import java.util.Comparator;
public class NameComparator implements Comparator<Employee>{
@Override
public int compare(Employee e1, Employee e2) {
int result = e1.getName().compareTo(e2.getName());
if(result==0)
return Integer.compare(e1.getId(), e2.getId());
return result;
}
}
Step-4 Experience Comparator
package com.silan;
import java.util.Comparator;
public class ExperienceComparator implements Comparator<Employee>{
@Override
public int compare(Employee e1, Employee e2) {
int result = Integer.compare(e1.getExperience(),
e2.getExperience());
if(result==0)
return Integer.compare(e1.getId(), e2.getId());
return result;
}
}
Step-5 EmployeeService
package com.silan;
import java.util.*;
public class EmployeeService {
TreeSet<Employee> employees = new TreeSet<>();
public void addEmployee(Employee e){
if(employees.add(e))
System.out.println("Employee Added Successfully.");
else
System.out.println("Duplicate Employee ID.");
}
public void removeEmployee(int id){
Employee emp = searchEmployee(id);
if(emp!=null){
employees.remove(emp);
System.out.println("Employee Removed.");
}
else{
System.out.println("Employee Not Found.");
}
}
public Employee searchEmployee(int id){
for(Employee e : employees){
if(e.getId()==id)
return e;
}
return null;
}
public void displayByID(){
System.out.println("\nEmployees Sorted By ID");
System.out.println("-----------------------------------------------");
System.out.printf("%-6s %-15s %-10s %-5s\n",
"ID","Name","Salary","Exp");
System.out.println("-----------------------------------------------");
for(Employee e:employees){
System.out.println(e);
}
}
public void displayBySalary(){
TreeSet<Employee> salarySet =
new TreeSet<>(new SalaryComparator());
salarySet.addAll(employees);
System.out.println("\nEmployees Sorted By Salary");
System.out.println("-----------------------------------------------");
System.out.printf("%-6s %-15s %-10s %-5s\n",
"ID","Name","Salary","Exp");
System.out.println("-----------------------------------------------");
salarySet.forEach(System.out::println);
}
public void displayByName(){
TreeSet<Employee> nameSet =
new TreeSet<>(new NameComparator());
nameSet.addAll(employees);
System.out.println("\nEmployees Sorted By Name");
System.out.println("-----------------------------------------------");
System.out.printf("%-6s %-15s %-10s %-5s\n",
"ID","Name","Salary","Exp");
System.out.println("-----------------------------------------------");
nameSet.forEach(System.out::println);
}
public void displayByExperience(){
TreeSet<Employee> expSet =
new TreeSet<>(new ExperienceComparator());
expSet.addAll(employees);
System.out.println("\nEmployees Sorted By Experience");
System.out.println("-----------------------------------------------");
System.out.printf("%-6s %-15s %-10s %-5s\n",
"ID","Name","Salary","Exp");
System.out.println("-----------------------------------------------");
expSet.forEach(System.out::println);
}
}
Step-6 Main Class
package com.silan;
import java.util.Scanner;
public class EmployeeManagement {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
EmployeeService service = new EmployeeService();
while(true){
System.out.println("\n========= Employee Management =========");
System.out.println("1. Add Employee");
System.out.println("2. Remove Employee");
System.out.println("3. Search Employee");
System.out.println("4. Display By ID");
System.out.println("5. Display By Salary");
System.out.println("6. Display By Name");
System.out.println("7. Display By Experience");
System.out.println("8. Exit");
System.out.print("Enter Choice : ");
int choice = sc.nextInt();
switch(choice){
case 1:
System.out.print("Employee ID : ");
int id = sc.nextInt();
sc.nextLine();
System.out.print("Employee Name : ");
String name = sc.nextLine();
System.out.print("Salary : ");
double salary = sc.nextDouble();
System.out.print("Experience : ");
int exp = sc.nextInt();
service.addEmployee(
new Employee(id,name,salary,exp));
break;
case 2:
System.out.print("Employee ID : ");
service.removeEmployee(sc.nextInt());
break;
case 3:
System.out.print("Employee ID : ");
Employee emp =
service.searchEmployee(sc.nextInt());
if(emp!=null)
System.out.println(emp);
else
System.out.println("Employee Not Found.");
break;
case 4:
service.displayByID();
break;
case 5:
service.displayBySalary();
break;
case 6:
service.displayByName();
break;
case 7:
service.displayByExperience();
break;
case 8:
System.out.println("Thank You");
System.exit(0);
default:
System.out.println("Invalid Choice");
}
}
}
}
Output
========= Employee Management =========
1. Add Employee
2. Remove Employee
3. Search Employee
4. Display By ID
5. Display By Salary
6. Display By Name
7. Display By Experience
8. Exit
Enter Choice : 1
Employee ID : 101
Employee Name : Amit
Salary : 45000
Experience : 3
Employee Added Successfully.
Display by Salary:
Employees Sorted By Salary
ID Name Salary Exp
------------------------------------------
104 Rahul 30000.00 1
101 Amit 45000.00 3
102 Priya 60000.00 5
103 Ramesh 85000.00 8
Display by Name:
Employees Sorted By Name
Amit
Priya
Rahul
Ramesh
Display by Experience:
Employees Sorted By Experience
Rahul
Amit
Priya
Ramesh
Concepts Covered
This project demonstrates several important Java concepts:
- Object-Oriented Programming (Encapsulation)
- TreeSet and the Collection Framework
- Comparable (natural ordering by Employee ID)
- Comparator (custom ordering by Salary, Name, and Experience)
- Menu-driven application design
- Searching, adding, removing, and displaying records
- Method decomposition and reusable service layer
- Formatted console output
- Java 8 method references (forEach(System.out::println))
This is an excellent practice project for Java Full Stack interviews because it closely resembles the type of coding exercise interviewers use to evaluate understanding of collections, sorting strategies, and object-oriented design.