使用它的唯一ID删除ArrayList的Object

时间:2017-10-09 11:38:26

标签: java arraylist

我遇到ArrayList的功能问题。我正在尝试通过指定ID来删除ArrayList的对象。我的createEmployee课程中有Main方法:

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
    }
}

我有ArrayList,我通过使用以下方法注册员工来添加员工(下面是removeEmployee方法)。

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);
            }
        }
    }
    */
}

我所知道的唯一方法就是编写employees.remove(index)并通过指定其索引来删除员工。 所以我想知道是否可以通过指定其唯一ID来删除员工。谢谢。

5 个答案:

答案 0 :(得分:4)

使用Java 8中引入的removeIf获取最短代码。

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

您可能还想考虑使用Map,因为ID是唯一的,然后您可以使用id非常轻松有效地访问(和/或删除)员工。您还可以使用Map.values()将所有员工作为集合(尽管不是List)。

Map<String, Employee> employees = new HashMap<>();
employees.put(e.getId().toString(), e); // Or use UUID directly as key
employees.remove(idToBeRemoved);

答案 1 :(得分:3)

从Java 8开始,有removeIf()方法。您可以按如下方式使用它:

employees.removeIf(employee -> employee.getId().equals(removeId));

答案 2 :(得分:1)

使用smth。那样:

public void removeEmployee(int removeId) {
 Employee empToRemove = employees.get(removeId);
 Iterator<Employee> iter = employees.iterator();
 while(iterator.hasNext()){
  Employee emp = iterator.next();
  if(emp.equals(empToRemove)){
   iterator.remove();
  }
 }
}

答案 3 :(得分:1)

是的,这是可能的。一种方法是使用Iterator。你使用了错误的语法。但你的方法是正确的。将其更改为

public void removeEmployee(String id){
    Iterator<Employee> it = employees.iterator();
    while(it.hashNext()){
        Employee employee = it.next();
        if(employee.getId().equals(id)){
            it.remove();
            break;
        }
    }
}

获取用户输入的调用将传递给方法removeEmployee

答案 4 :(得分:1)

以前的Java 8,我们必须使用remove方法。 由于ID是唯一的,因此这可能是equals实现。

public boolean equals(Object o){
    //check for instance and null

    Employee e = (Employee) o;
    return this.uid.equals(e.uid);
}

然后,只需致电ArrayList.remove(Object)

list.remove(employeeToRemove);

这不需要是同一个实例,只是为了拥有相同的UID,所以你可以简单地做

Employee employeeToRemove = new Employee(UUID)
list.remove(employeeToRemove);

创建一个删除另一个实例的实例可能有点无用,但是循环也没用,因为有方法可以执行它;)

注意,在实施equals的情况下,您应该实施hashcode以确保安全。

public int hashcode(){
    return UID.hashcode();
}

!我的所有代码都过于简单,并且不会检查null是否更短!