比较和删除列表中的元素

时间:2013-07-13 04:22:29

标签: java logic

我有两个列表,addListdeleteList

addList中的元素属于AddEntity类型,其中包含两个字段

- id 
- parentId

AddEntity类如下

public class AddEntity{
    int id;
    int parentId;

    //getters and setters here
}

deleteList中的实体属于DeleteEntity类型,只有一个字段

- deleteId

DeleteEntity类如下

public class DeleteList{
    int deleteId;
    //gettter and setter for deleteId goes here
}

现在我有两个清单 List<AddEntity> addListList<DeleteEntity> deleteList

对于Eg。 addList内容

id          parentId

2001          3
2002          2001
2003          2001
2004          2002
2005          2003
2006          4
2007          2006

deleteList内容

deleteId

2001
3
2007

现在我想从addList id/parentIddeleteId中的deleteList元素匹配的deleteList删除所有实体及其子项(递归)。 而且我还想保留addList中与addList中的任何ID都不匹配的参与者。

例如,在这种情况下,在处理上述两个列表之后,id parentId 2006 4 的内容应为

deleteList

deleteId 3 现在包含

Step1: For each elements in the deleteList{
        For each elements in the addList{
          a) Match deleteId with id of each element in addList.
             if(deleteId==id){
               mark current element from deleteList for deletion  
               loop:    check if any other element in addList has parentId==id.
                        if YES mark it(addList element) for delete
                        take the id of the marked element and goto "loop"

        }
      }      
Step2: Delete All Marked Elements!!

我有正确的逻辑,但在实现部分遇到一些问题。我是在JAVA做的。希望在这里找到一些解决方案。 谢谢!

编辑

- (因为有些人对这个问题感到不安)

MY APPROACH

实际上逻辑非常简单。但有点令人困惑。

foreach

首先,我尝试使用ConcurrentModificationException并删除列表中的元素,而不是将其标记为删除。这导致{{1}}。然后我使用Iterator迭代列表。这就是我陷入困境并提出这个问题的原因。

1 个答案:

答案 0 :(得分:1)

检查一下。在这里,我发布了我的整个代码,但我只测试了几个场景。欢迎所有评论。

AddList Class

public class AddList {
private int id;
private int parentId;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public int getParentId() {
    return parentId;
}

public void setParentId(int parentId) {
    this.parentId = parentId;
}
}

DeleteList Class

public class DeleteList {
private  int deleteId;

public int getDeleteId() {
    return deleteId;
}

public void setDeleteId(int deleteId) {
    this.deleteId = deleteId;
}
}

这是主类

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class Main {
public static void main(String[] args) throws ParseException {
    List<AddList> addList = new ArrayList<AddList>();
    List<DeleteList> delList = new ArrayList<DeleteList>();
    AddList addL1 = new AddList();
    AddList addL2 = new AddList();
    AddList addL3 = new AddList();
    AddList addL4 = new AddList();
    AddList addL5 = new AddList();
    AddList addL6 = new AddList();
    AddList addL7 = new AddList();

    DeleteList delL1 = new DeleteList();
    DeleteList delL2 = new DeleteList();
    DeleteList delL3 = new DeleteList();

    addL1.setId(2001);
    addL1.setParentId(3);
    addL2.setId(2002);
    addL2.setParentId(2001);
    addL3.setId(2003);
    addL3.setParentId(2001);
    addL4.setId(2004);
    addL4.setParentId(2002);
    addL5.setId(2005);
    addL5.setParentId(2003);
    addL6.setId(2006);
    addL6.setParentId(4);
    addL7.setId(2007);
    addL7.setParentId(2006);

    delL1.setDeleteId(2001);
    delL2.setDeleteId(3);
    delL3.setDeleteId(2007);

    addList.add(addL1);
    addList.add(addL2);
    addList.add(addL3);
    addList.add(addL4);
    addList.add(addL5);
    addList.add(addL6);
    addList.add(addL7);

    delList.add(delL1);
    delList.add(delL2);
    delList.add(delL3);

    removeElements(addList, delList);
}

public static void removeElements(List<AddList> add, List<DeleteList> del) {
    boolean status = true;
    int[] temp = new int[del.size()];
    int[] child = new int[add.size()];
    int i = 0;
    while (status) {
        for (int j = 0; j < add.size(); j++) {
            if (del.get(i).getDeleteId() == add.get(j).getId()) {
                add.remove(j);
                temp[i] = del.get(i).getDeleteId();
                j = -1;
            }
        }
        i++;
        if (i == del.size()) {
            status = false;
        }
    }
    i = 0;
    int k = 0;
    boolean newStatus = true;
    while (newStatus) {
        for (int j = 0; j < add.size(); j++) {
            if (temp[i] == add.get(j).getParentId()) {
                child[k] = add.get(j).getId();
                add.remove(j);
                k++;
                j = -1;
            }
        }
        i++;
        if (i == del.size()) {
            newStatus = false;
        }
    }
    i = 0;
    boolean con = true;
    while (con) {
        for (int j = 0; j < del.size(); j++) {
            if (temp[i] == del.get(j).getDeleteId()) {
                del.remove(j);
                j = -1;
            }
        }
        i++;
        if (i == temp.length) {
            con = false;
        }
    }

    i = 0;
    boolean cons = true;
    while (cons) {
        for (int j = 0; j < add.size(); j++) {
            if (child[i] == add.get(j).getParentId()) {
                add.remove(j);
                j = -1;
            }
        }
        i++;
        if (i == child.length) {
            cons = false;
        }
    }
}

}