多维arrayList获取ID并从arrayList中移除整个列表

Multidimensional arrayList get ID and remove the entire list from the arrayList

class Emp {

    String ID, FName, LName, ASalary, StartDate;

    Emp(String _ID, String _FName, String _LName, String _ASalary, String _StartDate) {
        ID = _ID;
        FName = _FName;
        LName = _LName;
        ASalary = _ASalary;
        StartDate = _StartDate;
    }
}

ArrayList< Emp> Record = new ArrayList< Emp>();


 private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {                                          
        String ID;

        ID = IDin.getText();
     
        int firstIndex = Record.indexOf(ID);
        Record.remove(firstIndex);
        Display.setText("remove " + ID + " Indext of " + firstIndex);
}      

它 returns -1 因为它在 ArrayList 中找不到 ID,我只需要获取 ID 号并删除具有该 ID 号的整个 ArrayList

您可以简单地使用 removeIf 并提供如下谓词:

id = IDin.getText();
record.removeIf(emp -> emp.getId().equals(id)); //add null check if needed

boolean removeIf(Predicate<? super E> filter)

Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.