从arraylist中删除重复的对象,并使用唯一对象合并数据

时间:2014-10-30 03:26:22

标签: java

请帮我解决以下问题,如果您有任何疑问,请与我们联系。

我在departmentList对象中有数据。 departmentList中有3个部门对象。对象1有2个员工记录。对象3是单独的dept对象,但具有相同的deptid。

(Deptobject DEPTID DEPTNAME Location EmpId EmpName Empsal)

<1> 1 ABC NY 1 David $ 5000

1 1 ABC NY 2 Sam $ 6000

2 2 PQR NC 4 Brian $ 5500

3 1 ABC NY 5 Glen $ 7000

我需要将数据合并到一个对象中(在唯一的deptid下)。

预期从上面的样本数据中输出: - (departmentList包含2个对象,object1(depid:1)应包含1个employeeelist对象下的3个雇员对象)

(Deptobject DEPTID DEPTNAME Location EmpId EmpName Empsal)

<1> 1 ABC NY 1 David $ 5000

1 1 ABC NY 2 Sam $ 6000

2 2 PQR NC 4 Brian $ 5500

1 1 ABC NY 5 Glen $ 7000

List<Department> departmentsList = new ArrayList<Department>(); 

public Department
 {
  int deptId;
  String deptName;
  String deptLocation;
  List<Employee> employeeList=new ArrayList<Employee>();

 getterxxx();
 setters();
private void addEmployee(Employee e)
{
 this.employeeList.add(e);
 }

}

 public class  Employee
 {
  int empId;
  String empName;
  int sal;
 }

提前致谢!

1 个答案:

答案 0 :(得分:0)

已经解决了。

    //Source arrayList
    ArrayList<Department> originalDepartmentsList=externalSys.getDepartmentsList();

    //for duplicate Department
    ArrayList<Department> duplicateDeptList = new ArrayList<Department>();

    //for unique Department
    HashMap<String,Department> uniqueDepts = new HashMap<String,Department>();

    //For duplicate identification and create a seperate list purpose
    Set<String> deptIdsList = new HashSet<String>();    

    Iterator originalIte=originalDepartmentsList.iterator();
    System.out.println("size of Department objs count in originalDepartmentsList.."+originalDepartmentsList.size());

    while(originalIte.hasNext())
    {
        Department deptObj=(Department) originalIte.next();
        String deptId=deptObj.getDeptId();
        if(!deptIdsList.add(deptId))
        {
            duplicateDeptList.add(deptObj);
            System.out.println("Duplicate SId is."+deptId);
        }
        else
        {
            System.out.println("deptId "+deptId+" is added to the uniques map.");
            uniqueDepts.put(deptId,deptObj);
        }       

    }

    System.out.println("duplicateDeptList size."+duplicateDeptList.size());
    System.out.println("uniqueDepts size."+uniqueDepts.size());
    System.out.println("********************************");

    //Adding Employee objects to the unique Department list.
    Iterator dupListIte=duplicateDeptList.iterator();
    while(dupListIte.hasNext())
    {
        Department deptObj=(Department) dupListIte.next();
        String deptId=deptObj.getDeptId();
        System.out.println("Duplicate DeptId."+deptId);

        ArrayList<Employee> employeeList= deptObj.getEmployeeList();

       if(employeeList!=null && employeeList.size()>0){

         Iterator empIte=employeeList.iterator();
        while(empIte.hasNext())
        {
            System.out.println("Employee list size before: for DeptId "+deptId+" .."+uniqueDepts.get(deptId).getEmployeeList().size());
            Employee empObj=(Employee) empIte.next();
            uniqueDepts.get(deptId).addToEmployeeList(empObj);
            System.out.println("Employee list size after: for DeptId "+deptId+" .."+uniqueDepts.get(deptId).getEmployeeList().size());
        }
}

}
列出finalDeptList = new ArrayList(uniqueDepts.values());