The following example code demonstrates How to Create ArrayList 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 ArrayList 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.

Solution

Here is the implementation in Java for the given problem statement.

import java.util.ArrayList;

class Emp {
    private int emp_id;
    private String name;
    private double salary;
    private String gender;
    private String department;
    private String email_id;
    
    public Emp(int emp_id, String name, double salary, String gender, String department, String email_id) {
        this.emp_id = emp_id;
        this.name = name;
        this.salary = salary;
        this.gender = gender;
        this.department = department;
        this.email_id = email_id;
    }
    
    public int getEmp_id() {
        return emp_id;
    }
    
    public void setEmp_id(int emp_id) {
        this.emp_id = emp_id;
    }
    
    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 String getGender() {
        return gender;
    }
    
    public void setGender(String gender) {
        this.gender = gender;
    }
    
    public String getDepartment() {
        return department;
    }
    
    public void setDepartment(String department) {
        this.department = department;
    }
    
    public String getEmail_id() {
        return email_id;
    }
    
    public void setEmail_id(String email_id) {
        this.email_id = email_id;
    }
    
    @Override
    public String toString() {
        return "Emp ID: " + emp_id + ", Name: " + name + ", Salary: " + salary + ", Gender: " + gender + ", Department: " + department + ", Email: " + email_id;
    }
}

class EmpList {
    private ArrayList<Emp> empList;
    
    public EmpList() {
        empList = new ArrayList<>();
    }
    
    public void addEmp(Emp ob) {
        empList.add(ob);
    }
    
    public void deleteEmp(int eid) {
        for (int i = 0; i < empList.size(); i++) {
            if (empList.get(i).getEmp_id() == eid) {
                empList.remove(i);
                break;
            }
        }
    }
    
    public void displayMinimumSalary() {
        double minSalary = Double.MAX_VALUE;
        for (Emp e : empList) {
            if (e.getSalary() < minSalary) {
                minSalary = e.getSalary();
            }
        }
        System.out.println("Minimum Salary: " + minSalary);
    }



Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise

programmingempire

Princites