The following example code demonstrates How to Create a LinkedList of Objects in Java.

Define a class Emp with the following fields – emp_id, name, salary, gender, department, and email_id. Also, define the corresponding constructors, and getter and setter methods. Further, override the toString() method to display the details of the employee. Now create another class EmpList, that has an LinkedList as its data member. In this class, define two methods – addEmp(Emp ob), and deleteEmp(int eid) that adds and removes an Emp object in the list. Also, create methods to display minimum salary, maximum salary, average salary, and search an employee using the given emp_id. Finally, test the program using the main() method.

Here is an example implementation of the classes in Java.





import java.util.LinkedList;

class Emp {
  private int empId;
  private String name;
  private double salary;
  private char gender;
  private String department;
  private String emailId;

  public Emp(int empId, String name, double salary, char gender, String department, String emailId) {
    this.empId = empId;
    this.name = name;
    this.salary = salary;
    this.gender = gender;
    this.department = department;
    this.emailId = emailId;
  }

  public int getEmpId() {
    return empId;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public double getSalary() {
    return salary;
  }

  public void setSalary(double salary) {
    this.salary = salary;
  }

  public char getGender() {
    return gender;
  }

  public void setGender(char gender) {
    this.gender = gender;
  }

  public String getDepartment() {
    return department;
  }

  public void setDepartment(String department) {
    this.department = department;
  }

  public String getEmailId() {
    return emailId;
  }

  public void setEmailId(String emailId) {
    this.emailId = emailId;
  }

  @Override
  public String toString() {
    return "Employee ID: " + empId + ", Name: " + name + ", Salary: " + salary + ", Gender: " + gender
        + ", Department: " + department + ", Email ID: " + emailId;
  }
}

class EmpList {
  private LinkedList<Emp> list;

  public EmpList() {
    list = new LinkedList<>();
  }

  public void addEmp(Emp emp) {
    list.add(emp);
  }

  public void deleteEmp(int eid) {
    for (Emp emp : list) {
      if (emp.getEmpId() == eid) {
        list.remove(emp);
        break;
      }
    }
  }

  public void displayMinSalary() {
    double minSalary = Double.MAX_VALUE;
    for (Emp emp : list) {
      if (emp.getSalary() < minSalary) {
        minSalary = emp.getSalary();
      }
    }
    System.out.println("Minimum salary: " + minSalary);
  }

  public void displayMaxSalary() {
    double maxSalary = Double.MIN_VALUE;
    for (Emp emp : list) {
      if (emp.getSalary() > maxSalary) {
        maxSalary = emp.getSalary();
      }
    }
    System.out.println("Maximum salary: " + maxSalary);
  }

  public void displayAverageSalary() {
   // double
// Write your code here
}

}

Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise

programmingempire

Princites