Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过比较两个列表从一个列表中删除元素_Java_List_Arraylist - Fatal编程技术网

Java 如何通过比较两个列表从一个列表中删除元素

Java 如何通过比较两个列表从一个列表中删除元素,java,list,arraylist,Java,List,Arraylist,我有两个列表,比如列表1和列表2。 如果列表1中已经存在元素,我需要从列表2中删除这些元素 为了避免ConCurrentModificationException,我尝试使用ListIterator employeeList1 // data present employeeList2 // data present ListIterator<Employee> zeroList=employeeList2.listIterator(); //remove employee if

我有两个列表,比如列表1和列表2。 如果列表1中已经存在元素,我需要从列表2中删除这些元素

为了避免ConCurrentModificationException,我尝试使用ListIterator

employeeList1 // data present
employeeList2 // data present
ListIterator<Employee> zeroList=employeeList2.listIterator();

//remove employee if already present in list1
while(zeroList.hasNext()){
for(Employee employee : employeeList1){
                    if(zeroList.next().getID().equals(employee.getId())){
zeroList.remove();
                    }
                }
            }

元素可能不在列表1中,但检查条件是必要的。

您可以对要从中删除元素的集合使用
removeAll
方法,并将该集合作为包含要删除元素的参数传递

List<String> list1 = new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
List<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("p");
list2.add("q");
list2.removeAll(list1);
System.out.println(list2);
编辑:这里是
Employee
类的示例代码,而您的可能是另一个,但正如您所说,您的密钥是
employeeId
,因此
等于
hashCode
方法只需要在
employeeId
上使用

static class Employee {
    private long employeeId;
    private String name;
    // whatever more variables

    public Employee(long employeeId, String name) {
        this.employeeId = employeeId;
        this.name = name;
    }

    public String toString() {
        return String.format("Employee[employeeId=%s, name=%s]", employeeId, name);
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Employee) {
            return this.employeeId == ((Employee) o).employeeId;
        }
        return false;
    }

    @Override
    public int hashCode() {
        return new Long(employeeId).hashCode();
    }
}

public static void main(String[] args) throws Exception {
    List<Employee> list1 = new ArrayList<>();
    list1.add(new Employee(1, "a"));
    list1.add(new Employee(2, "b"));
    list1.add(new Employee(3, "c"));
    List<Employee> list2 = new ArrayList<>();
    list2.add(new Employee(1, "a"));
    list2.add(new Employee(4, "d"));
    list2.add(new Employee(5, "e"));
    list2.removeAll(list1);
    System.out.println(list2);
}
静态类员工{
私人长期雇员ID;
私有字符串名称;
//还有什么变数
公共雇员(长employeeId,字符串名称){
this.employeeId=employeeId;
this.name=名称;
}
公共字符串toString(){
返回String.format(“Employee[employeeId=%s,name=%s]”,employeeId,name);
}
@凌驾
公共布尔等于(对象o){
if(o员工实例){
返回this.employeeId==((Employee)o).employeeId;
}
返回false;
}
@凌驾
公共int hashCode(){
返回新的Long(employeeId).hashCode();
}
}
公共静态void main(字符串[]args)引发异常{
List list1=新的ArrayList();
列表1.添加(新员工(1,“a”);
列表1.添加(新员工(2,“b”);
列表1.添加(新员工(3,“c”);
List list2=新的ArrayList();
清单2.添加(新员工(1,“a”);
清单2.添加(新员工(4,“d”);
清单2.添加(新员工(5,“e”);
清单2.移除所有(清单1);
System.out.println(列表2);
}

尝试使用此代码并查看打印内容,然后对
equals
hashCode
方法进行注释,然后查看发生了什么。注释这两个方法后,列表1中的对象不会被删除,因为列表不知道这两个对象何时相等。

简单的方法有什么问题

List<Employee> employeeList1
List<Employee> employeeList2

for (Employee employee : employeeList2) {
   if (employeeList1.contains(employee)) {
      employeeList2.remove(employee)
   }
}
列出员工列表1
员工名单2
适用于(员工:员工列表2){
if(employeeList1.contains(employee)){
employeeList2.删除(员工)
}
}
当然,如果使用多个线程,则必须同步列表调用。

您可以使用

图书馆:

List<String> newList = ListUtils.union(list1, list2);
List newList=ListUtils.union(list1,list2);
employeeList1//存在数据
employeeList2//存在数据
List newList=newarraylist();
newList.add(employeeList2);
新名单。保留(员工名单1);

新列表将不包含employeeList1数据。

My Employee实体包含多个变量。因此,通过对象删除可能并不总是有效。我需要根据EmployeeId删除即使列表中存储了自定义对象,您仍然可以使用相同的方法。只要确保在对象存储在列表中的类中实现
equals()
hashCode()
方法。正如您所说的基于employeeId,只需实现基于
employeeId的
equals
hashCode
两种方法,您可以在帖子中添加自定义类的代码吗?我会给你们一些具体的指针。因为我在这里使用arraylist,所以有必要重写hashcode吗。。。我只需要从列表2中删除“a”,而不需要从列表1中删除。意味着列表1中的数据应该从列表2中删除,以防出现小混乱。只重写
equals()
方法应该足够了,因为您没有使用任何确实使用
hashCode()方法的
HashMap
HashSet
。但一般来说,一个好的做法是总是同时实现
equals
hashCode
,这样,如果对象在散列数据结构中的某个位置用作键,它的行为就会一致且良好。My Employee实体包含多个变量。因此,通过对象删除可能并不总是有效。我需要根据EmployeeId删除,如果EmployeeId相等,是否以employee1.equals(employee2)的方式实现Employee.equals()?或者你需要其他地方的平等吗?欢迎来到这里!虽然这几乎解决了这个问题,但是您能解释一下您的代码是怎么做的吗?即使没有经验的用户也能理解它?谢谢
List<Employee> employeeList1
List<Employee> employeeList2

for (Employee employee : employeeList2) {
   if (employeeList1.contains(employee)) {
      employeeList2.remove(employee)
   }
}
List<String> newList = ListUtils.union(list1, list2);
employeeList1 // data present
employeeList2 // data present
List<Employee> newList = new ArrayList<Empolyee>();
newList.add(employeeList2 );
newList.retainAll(employeeList1);