从列表中删除唯一记录

发布于 2024-09-14 01:51:15 字数 252 浏览 2 评论 0原文

我有一个员工列表。我的员工类别如下

public class Employee{
  int empid;
  String name;
  ...
}

,现在我想从列表中删除 empid 为 5 的员工。一种方法是迭代列表并检查 empid == 5。是否还有其他方法我能做到吗?

还希望我的列表包含具有唯一 empid 的员工。任何添加具有重复 empid 的员工的尝试都应该抛出异常。如何做到这一点?

i have a list of employees.My employee class is as follows

public class Employee{
  int empid;
  String name;
  ...
}

now i want to remove a employee from list whose empid is 5.One way to do is to iterate the list and check if empid == 5.Is there any other way by which i can do it?

Also is want my list to contain employees with unique empid.Any attempt made to add employees with duplicate empid should throw an exception.How to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燕归巢 2024-09-21 01:51:15

而不是 List< Employee>,使用Set<员工>
不要忘记重写 Employee 类的 hashCode()equals() 方法。

Instead of List< Employee>, use Set< Employee>.
Don't forget to override hashCode() and equals() methods of your Employee class.

空城之時有危險 2024-09-21 01:51:15

如果员工的顺序相关(或者如果您需要能够让一名员工多次出现),则需要将它们存储在列表中。 (否则 Set 就足够了。)

我会让 Employee 重写 equals 方法并使用 List.remove(Object o)

来自 List 的 API 文档:

布尔删除(对象o)
从此列表中删除第一次出现的指定元素(如果存在)(可选操作)。如果此列表不包含该元素,则它不会更改。更正式地说,删除具有最低索引 i 的元素,使得 (o==null ? get(i)==null : o.equals(get(i))) (如果存在这样的元素)。

具体来说,你可以这样做

public class Employee{

    int empid;
    String name;

    public boolean equals(Object o) {
        if (o == null || !(o instanceof Employee))
            return false;
        Employee e = (Employee) o;
        return empid == e.empid && name.equals(e.name);
    }

    public int hashCode() {
        return empid ^ name.hashCode();
    }
}

If the order of the employees is of relevance (or if you need to be able to let one employee be represented multiple times) you need to store them in a list. (Otherwise a Set would suffice.)

I would let Employee override the equals method and use List.remove(Object o).

From the API docs of List:

boolean remove(Object o)
Removes the first occurrence of the specified element from this list, if it is present (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Concretely, you could do something like

public class Employee{

    int empid;
    String name;

    public boolean equals(Object o) {
        if (o == null || !(o instanceof Employee))
            return false;
        Employee e = (Employee) o;
        return empid == e.empid && name.equals(e.name);
    }

    public int hashCode() {
        return empid ^ name.hashCode();
    }
}
难理解 2024-09-21 01:51:15

对于问题的第一部分,您可以调用remove(),传入一个 Employee 对象,该对象的 equals() 方法对于 id 5 的 Employee 返回 true。

对于第二部分,而不是 ListSet 保证没有任何重复项。你的收藏有必要是一个列表吗?

For the first part to your question, you can call remove() passing in an Employee object whose equals() method returns true for an Employee of id 5.

For your second part, instead of a List, a Set guarantees not to have any duplicates. Is it necessary for your collection to be a list?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文