Code Example: Using ArrayList and Iterator

This code example demonstrates the use of ArrayList and Iterator in Java to manage a list of employees. It includes operations such as displaying all employees, filtering employees based on a salary condition, and increasing the salary of all employees by a specified percentage.

This example showcases advanced list manipulation techniques using Iterator and Predicate, highlighting how to filter and modify elements in an ArrayList effectively.

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

public class AdvancedArrayListIteratorExample {
    public static void main(String[] args) {
        // Creating an ArrayList of custom objects
        List<Employee> employees = new ArrayList<>();
        employees.add(new Employee(1, "Alice", 25000));
        employees.add(new Employee(2, "Bob", 30000));
        employees.add(new Employee(3, "Charlie", 28000));
        employees.add(new Employee(4, "David", 32000));
        employees.add(new Employee(5, "Eve", 29000));

        // Displaying all employees
        System.out.println("All Employees:");
        displayEmployees(employees);

        // Filtering employees with salary greater than 28000
        Predicate<Employee> highSalaryPredicate = e -> e.getSalary() > 28000;
        List<Employee> highSalaryEmployees = filterEmployees(employees, highSalaryPredicate);

        // Displaying employees with salary greater than 28000
        System.out.println("\nEmployees with salary greater than 28000:");
        displayEmployees(highSalaryEmployees);

        // Increasing the salary of all employees by 10%
        System.out.println("\nIncreasing the salary of all employees by 10%:");
        increaseSalary(employees, 10);
        displayEmployees(employees);
    }

    // Method to display employees
    public static void displayEmployees(List<Employee> employees) {
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    // Method to filter employees based on a condition
    public static List<Employee> filterEmployees(List<Employee> employees, Predicate<Employee> predicate) {
        List<Employee> filteredList = new ArrayList<>();
        Iterator<Employee> iterator = employees.iterator();
        while (iterator.hasNext()) {
            Employee employee = iterator.next();
            if (predicate.test(employee)) {
                filteredList.add(employee);
            }
        }
        return filteredList;
    }

    // Method to increase the salary of all employees by a certain percentage
    public static void increaseSalary(List<Employee> employees, double percentage) {
        Iterator<Employee> iterator = employees.iterator();
        while (iterator.hasNext()) {
            Employee employee = iterator.next();
            double newSalary = employee.getSalary() * (1 + percentage / 100);
            employee.setSalary(newSalary);
        }
    }
}

// Employee class representing an employee
class Employee {
    private int id;
    private String name;
    private double salary;

    public Employee(int id, String name, double salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

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

    @Override
    public String toString() {
        return "Employee{id=" + id + ", name='" + name + "', salary=" + salary + "}";
    }
}

Explanation

Employee Class

  • Employee Class: Represents an employee with fields for ID, name, and salary. Includes getter and setter methods and an overridden toString() method for easy printing.

Main Class (AdvancedArrayListIteratorExample)

  • Creating ArrayList: Initializes an ArrayList of Employee objects with sample data.
  • Display Employees: Uses the displayEmployees method to print all employees.
  • Filtering Employees: Uses the filterEmployees method to create a list of employees with a salary greater than 28,000. The method accepts a Predicate to define the filter condition.
  • Increasing Salary: Uses the increaseSalary method to increase each employee’s salary by a specified percentage.

Methods

  • displayEmployees: Iterates through the list and prints each employee.
  • filterEmployees: Iterates through the list, applies the filter condition, and collects matching employees into a new list.
  • increaseSalary: Iterates through the list and increases each employee’s salary by the specified percentage.

Key Takeaways

  • Advanced Use of ArrayList and Iterator: Demonstrates filtering and modifying elements in an ArrayList.
  • Predicate Functional Interface: Utilizes Java’s Predicate interface to define filter conditions dynamically.
  • Enhanced Readability and Reusability: Breaks down operations into methods for better readability and reusability.