简体   繁体   English

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

[英]How to remove element from one list by comparing two lists

I have two Lists, say List1 and List2.我有两个列表,比如说 List1 和 List2。 I need to remove elements from List2 if that is already present in List1如果 List1 中已经存在元素,我需要从 List2 中删除它

To avoid ConCurrentModificationException, I have tried with ListIterator为了避免 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();
                    }
                }
            }

I am getting the below exception inside the if condition我在 if 条件中收到以下异常

java.util.NoSuchElementException

It is possible that element might not be present in List1, but checking the condition is necessary.元素可能不存在于 List1 中,但检查条件是必要的。

You can use removeAll method on collection from which you want to remove the elements and pass the collection as arguments which contains the elements you want to remove.您可以对要从中删除元素的集合使用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);

Removes a from list2 as it was present in list1 and prints p and q only,从 list2 中删除a ,因为它存在于 list1 中并仅打印pq

[p, q]

Edit: Here is a sample code for Employee class while yours might be a different one but as you said your key is employeeId hence equals and hashCode methods needs to play on employeeId only.编辑:这是Employee类的示例代码,而您的类可能不同,但正如您所说,您的密钥是employeeId因此equalshashCode方法只需要在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);
}

Try using this code and see what it prints and then just comment both equals and hashCode method and then see what happens.尝试使用此代码并查看它打印的内容,然后只注释equalshashCode方法,然后看看会发生什么。 After you comment those two methods, the object present in list1 will not be removed as list doesn't know when the two objects are equal.注释这两个方法后,list1 中的对象不会被删除,因为 list 不知道这两个对象何时相等。

What is wrong with the simpler简单的有什么问题

List<Employee> employeeList1
List<Employee> employeeList2

for (Employee employee : employeeList2) {
   if (employeeList1.contains(employee)) {
      employeeList2.remove(employee)
   }
}

Of course you would have to synchronize the List calls if using multiple threads.当然,如果使用多个线程,则必须同步 List 调用。

You could use the Apache commons-collections您可以使用Apache commons-collections

library:图书馆:

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);

newList will not contain the employeeList1 data. newList 将不包含 employeeList1 数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM