Java – List of Employee Objects

List of Objects:

  • Collections are mainly used to store and process information of Employees, Students, Customers, Products, Books, Accounts etc.
  • Object is a set of dissimilar elements. For example, Employee has ID, Name and Salary.
  • We create objects with details and store the object into collection as follows.

Program to create and display List of Employees:

  1. Employee.java: contains Employee class
  2. Main.java: contains code of creating ArrayList with Employees and display.

Employee.java:

  • Create Employee class with instance variables id, name, salary
  • Define parameterized constructor to initialize the object.
class Employee
{
            int id;
            String name;
            double salary;
            Employee(int id, String name, double salary) {
                        this.id = id;
                        this.name = name;
                        this.salary = salary;
            }
}

Main.java:

  • Create 3 Employee objects and add to List
  • Display details using for-each loop
import java.util.*;
class Main
{
            public static void main(String[] args) {
                        List<Employee> list = new ArrayList<Employee>();
 
                        Employee e1 = new Employee(101, “Amar”, 35000);
                        Employee e2 = new Employee(102, “Harin”, 45000);
                        Employee e3 = new Employee(103, “Satya”, 40000);
                        list.add(e1);
                        list.add(e2);
                        list.add(e3);
                        System.out.println(“Details are : “);
                        for(Employee e : list)
                        {
                                    System.out.println(e.id + ” , ” + e.name + ” , ” + e.salary);
                        }
            }
}

You can directly add objects to the list as follows:

import java.util.*;
class Main
{
            public static void main(String[] args) {
                        List<Employee> list = new ArrayList<Employee>();
 
                        list.add(new Employee(101, “Amar”, 35000));
                        list.add(new Employee(102, “Harin”, 45000));
                        list.add(new Employee(103, “Satya”, 40000));
 
                        System.out.println(“Details are : “);
                        for(Employee e : list)
                        {
                                    System.out.println(e.id + ” , ” + e.name + ” , ” + e.salary);
                        }
            }
}

Display using for loop:

System.out.println(“Details are : “);
for(int i=0 ; i<=list.size()-1 ; i++)
{
            Employee e = list.get(i);
            System.out.println(e.id + ” , ” + e.name + ” , ” + e.salary);
}

Display Employees List in reverse order:

  • We must use for() loop to iterate in reverse order.
  • For-each loop can move only in forward direction.
System.out.println(“Details are : “);
for(int i=list.size()-1 ; i>=0 ; i–)
{
            Employee e = list.get(i);
            System.out.println(e.id + ” , ” + e.name + ” , ” + e.salary);
}

Display using Iterator:

System.out.println(“Details are : “);
Iterator<Employee> itr = list.iterator();
while(itr.hasNext())
{
            Employee e = itr.next();
            System.out.println(e.id + ” , ” + e.name + ” , ” + e.salary);
}

Display reverse list using ListIterator:

System.out.println(“Details are : “);
ListIterator<Employee> itr = list.listIterator(list.size());
while(itr.hasPrevious())
{
            Employee e = itr.previous();
            System.out.println(e.id + ” , ” + e.name + ” , ” + e.salary);
}
Scroll to Top