用于在集合java

时间:2015-09-10 11:44:20

标签: java algorithm collections recurrence

我有一种情况,我正在节省部门和子部门和员工。 我的数据库结构是顶级部门的父ID是0。 对于它下面的子部门,他们的父ID是顶级部门的主要关键,因此它成为了无限的子部门链。

PK | Department | Parent Id
----------------------------
1  | dept1      | 0
2  | dept2      | 0
3  | dept3      | 0
4  | sub_dept1  | 1
5  | sub_dept2  | 1
6  | sub_dept3  | 2
7  | sub_dept4  | 2
8  | sub_dept5  | 2
9  | sub_dept6  | 3
10 | sub_dept7  | 3

我创建了以下类:

Public Class Departments{
  private String name;
  private Arraylist<Departments> subDepts;
  private Arraylist<Employees> employees;
}


public void parseDepartmentsAndPopulate(Account account, StringBuffer sb){

    try {
        String jsonStr =sb.toString();

        Log.i("DownloadManager", "jsonStr value:" + jsonStr);

        Gson gson = new Gson();
        Type listType = new TypeToken<ArrayList<Model_Departments>>()
                {
                }.getType();

                JSONObject jsonObj = new JSONObject(jsonStr);
                JSONArray getArray = jsonObj.getJSONArray("groups");

                list_departments = (ArrayList<Model_Departments>) gson.fromJson(getArray+"", listType);
                //Type listType1 = new TypeToken<ArrayList<Model_SubDept>>() {
                //}.getType();
                Log.v("Model_Departments Model", list_departments.toString() );
                Log.v("Model_Departments", list_departments.size()+"length" );
                long autoIncrementId;
                for(Model_Departments m:list_departments)
                {
                    Log.v(LOGTAG, m.name);                      
                    map_departments.put(m.name, "");
                    DepartmentLevels d=new DepartmentLevels(m.name, 0);
                    autoIncrementId=databaseBackend.createDepartment(d);
                    list_topDepartments.add(d);
                    addSubgroups(m,autoIncrementId, account);
                }



                List<DepartmentLevels> list=databaseBackend.getDepartmentLevels();
                for(DepartmentLevels l:list){
                    Log.v(LOGTAG,l.getDepartment_name()+" parent id="+l.getParent_id());
                }
                for (String name: map_departments.keySet()){

                    String key =name.toString();
                    String value = map_departments.get(name).toString();  
                    System.out.println(key + " " + value);  


                } 


                /*for(Model_Departments m:list_departments){
                    if(m.subgroups.size()>0){
                        for(Model_Departments msd:m.subgroups)
                            databaseBackend.createDepartment(new DepartmentLevels(msd.name, 0));

                    }
                }*/

                updateRosterUi();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}
public void addSubgroups(Model_Departments m,long parentId, Account account)
{   
    if(m.subgroups!=null && m.subgroups.size()>0)
    {
        if(!map_departments.containsKey(m.name))
        {
            map_departments.put(m.name, "");                
        }
        for(Model_Departments msd:m.subgroups)
        {       

            long autoIncrementId=databaseBackend.createDepartment(new DepartmentLevels(m.name, parentId));
            Log.v(LOGTAG,"Model_Subgroups Groups :"+msd.name+" and Autoincrement ID="+autoIncrementId +" Parent Id= "+parentId);
            if(map_departments.containsKey(m.name))
            {
                String value=map_departments.get(m.name);
                map_departments.put(m.name,value.equals("") ? msd.name :value+","+ msd.name );
            }           
            addSubgroups(msd, autoIncrementId,account);
            //Log.v(LOGTAG,"Model_Subgroups Groups"+msd.name+" and Autoincrement ID="+autoIncrementId +" Parent Id= "+parentId);

        }
    }
    if(m.employee!=null && m.employee.size()>0)
    {
        if(m.employee!=null && m.employee.size()>0){
            for(Model_Employees me:m.employee)
            {
                String jid = me.name+"@"+Config.SERVER;
                String name = me.name;
                String subscription = "both";
                Contact contact = account.getRoster().getContact(jid);
                if(contact==null)
                {
                    String cleanJid =me.name+"@"+Config.SERVER;
                    contact = new Contact(cleanJid);
                    contact.setAccount(account);
                    contact.setLocality(me.location);
                    contact.setOrgUnit(m.name);
                }
                //Log.v("IQParser", "inside subscription of contact");
                if (!contact.getOption(Contact.Options.DIRTY_PUSH)) 
                {
                    if(name!=null && !name.equals(""))
                        contact.setServerName(name);
                    //else
                    //contact.setServerName(jid.substring(jid.indexOf("@")+1, jid.indexOf("/")));
                }
                if (subscription!=null) 
                {
                    if (subscription.equals("remove")) 
                    {
                        contact.resetOption(Contact.Options.IN_ROSTER);
                        contact.resetOption(Contact.Options.DIRTY_DELETE);
                        contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
                    }
                    else 
                    {
                        contact.setOption(Contact.Options.IN_ROSTER);
                        contact.resetOption(Contact.Options.DIRTY_PUSH);                            
                        contact.setOption(Contact.Options.TO);
                        contact.setOption(Contact.Options.FROM);
                        contact.resetOption(Contact.Options.PREEMPTIVE_GRANT);
                    }
                }
                Log.v(LOGTAG,"Model_Subgroups employee"+me.name);
            }


        }
    }
}

当我在hashmap中迭代这些嵌套值时,我很震惊。我无法创建数据结构,它可以保存Departments,它们下面的子部门和子部门下的员工的树视图。 任何人都可以帮我构建集合数据结构并帮助我迭代它。

1 个答案:

答案 0 :(得分:1)

这就是我填写Departement Class的方式

eclipse