Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing Object of ArrayList by using it's unique ID

Tags:

java

arraylist

I am having problems with the functions of the ArrayList. I'm trying to remove objects of an ArrayList by specifying their ID. I have a createEmployee method in my Main class:

public void createEmployee(){
    String typeofemployee = sc.nextLine();

    UUID uniqueID = UUID.randomUUID();
    String x = "" + uniqueID;
    System.out.println("The new ID of the " + typeofemployee + " is: " + uniqueID + ".");

    System.out.println("What's the name of the new " + typeofemployee + "?");
    name = sc.nextLine();
    System.out.println("What's the salary of the new " + typeofemployee + "?");
    salary = sc.nextDouble();
    Employee employee = new Employee(x, name, salary);

    switch (typeofemployee) {

        case "Employee":
            reusaxcorp.registerEmployee(employee);
            break;

        // other cases
    }
}

And I have an ArrayList, to which I add employees by registering them with the following method (down below there's the removeEmployee method).

public class ReusaxCorp extends Main {

Scanner input;
ArrayList<Employee> employees = new ArrayList<Employee>();
final String END_OF_LINE = System.lineSeparator();

public void registerEmployee(Employee employee){
    employees.add(employee);
}

public void retrieveEmployee() {
    for(Employee employee: employees){
        System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
        System.out.println(); // an empty line for each employee
    }
}

public void removeEmployee(){
    employees.remove(0); 
    /* I also tried this, but it doesn't work either
    Iterator<Employee> iter = employees.iterator();
    while(iter.hasNext()){
        for (int i = 0; i < employees.size(); i++) {
            System.out.println("Which eployee do you want to remove? Type in his/her ID. ");
            int number = input.nextInt();
            Employee employee = iter.next();
            if (employees.equals(number)) {
                employees.remove(i);
            }
        }
    }
    */
}

The only way I know of is just to write employees.remove(index) and removing an employee by specifying his index. So I want to know if it's possible to remove an employee by specifying his unique ID. Thank you.

like image 533
Mahatir Said Avatar asked Oct 27 '25 13:10

Mahatir Said


2 Answers

Use the removeIf introduced in Java 8 for the shortest code.

employees.removeIf(e -> e.getId().equals(id));

You might also want to consider using a Map, since the ids are unique and then you can access (and/or remove) an employee very easily and efficiently with just the id. You can also use Map.values() to get all the employees as a collection (though not a List).

Map<String, Employee> employees = new HashMap<>();
employees.put(e.getId().toString(), e); // Or use UUID directly as key
employees.remove(idToBeRemoved);
like image 78
Kayaman Avatar answered Oct 30 '25 04:10

Kayaman


Since Java 8 there is a removeIf() method. You can use it as follows:

employees.removeIf(employee -> employee.getId().equals(removeId));
like image 32
M. le Rutte Avatar answered Oct 30 '25 05:10

M. le Rutte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!