论坛首页 Java企业应用论坛

请教一个算法问题。

浏览 2538 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2014-09-14  

请教一个算法问题,有一个集合,List<DeptUserModel> duList;

DeptUserModel,如下图所示:

public class DeptUserModel {
	private String goupName;
	private List<String> userid;
	public String getGoupName() {
		return goupName;
	}
	public void setGoupName(String goupName) {
		this.goupName = goupName;
	}
	public List<String> getUserid() {
		return userid;
	}
	public void setUserid(List<String> userid) {
		this.userid = userid;
	}
	@Override
	public String toString() {
		return "DeptUserModel [goupName=" + goupName + ", userid=" + userid
				+ "]";
	}
	
}

现在我需要把duList里面所有goupName相同的DeptUserModel合并。以下是我的解决方案,求复制到低一点的解决方法。 

 

/**
	 * 合并 List<DeptUserModel> 把具有相同部门的用户合并
	 * @param duList
	 * @return
	 */
	public List<DeptUserModel> getDeptUserModelByDeptUserModel( List<DeptUserModel> duList){
		//合并DeptUserModel,把所有部门相同的合并到一起
		List<DeptUserModel> duListTemp = new ArrayList<DeptUserModel>();
		for (DeptUserModel deptUserModel : duList) {
			if(duListTemp.size()<1){
				duListTemp.add(deptUserModel);
			}else{
				DeptUserModel dum = new DeptUserModel();
				boolean ifAdd = false;
				for (DeptUserModel deptUserModelTemp : duListTemp){
					if(deptUserModelTemp.getGoupName().equals(deptUserModel.getGoupName())){
						
						List<String> list = new ArrayList<String>();
						list.addAll(deptUserModelTemp.getUserid());
						list.addAll(deptUserModel.getUserid());
						deptUserModelTemp.setUserid(list);
						ifAdd = false;
						
					}else if(!deptUserModelTemp.getGoupName().equals(deptUserModel.getGoupName())){
						dum = deptUserModel;
						ifAdd = true;
					}
				}
				if(ifAdd){
					duListTemp.add(dum);
				}
					
			}
					
		}
		return duListTemp;
	}
	

 

   发表时间:2014-09-18  
public List<DeptUserModel> getDeptUserModelByDeptUserModel(
		List<DeptUserModel> duList) {
	Map<String, List<String>> groupUsers = new HashMap<String, List<String>>();
	for (DeptUserModel deptUserModel : duList) {
		String groupName = deptUserModel.getGoupName();
		if (groupUsers.containsKey(groupName) == false) {
			groupUsers.put(groupName, new ArrayList<String>());
		}
		groupUsers.get(groupName).addAll(deptUserModel.getUserid());
	}
	List<DeptUserModel> result = new ArrayList<DeptUserModel>();
	for (Map.Entry<String, List<String>> entry : groupUsers.entrySet()) {
		DeptUserModel deptUserModel = new DeptUserModel();
		deptUserModel.setGoupName(entry.getKey());
		deptUserModel.setUserid(entry.getValue());
		result.add(deptUserModel);
	}
	return result;
}
0 请登录后投票
   发表时间:2014-09-20   最后修改:2014-09-20
import java.util.ArrayList;
import java.util.List;

public class TestList {

	public static void main(String[] args) {
		List<DeptUserModel> duList = new ArrayList<DeptUserModel>();
		// 1.
		duList.add(new DeptUserModel("00001", "A"));
		duList.add(new DeptUserModel("00002", "C"));
		duList.add(new DeptUserModel("00003", "A"));
		duList.add(new DeptUserModel("00023", "C"));
		duList.add(new DeptUserModel("00061", "B"));
		System.out.println("处理前:");
		printList(duList);
		// 0--
		getDeptUserModelByDeptUserModel(duList);
		// ---
		System.out.println("处理后:");
		printList(duList);
	}

	public static void printList(List<DeptUserModel> duList) {
		for (DeptUserModel dum : duList) {
			System.out.println(dum);
		}
	}

	public static void getDeptUserModelByDeptUserModel(
			List<DeptUserModel> duList) {
		for (int i = 0; i < duList.size(); i++) {
			DeptUserModel dum_i = duList.get(i);
			for (int j = i + 1; j < duList.size(); j++) {
				DeptUserModel dum_j = duList.get(j);
				if (dum_i.getGroupName().equals(dum_j.getGroupName())) {
					String userId = dum_i.getUserId() + "," + dum_j.getUserId();
					dum_i.setUserId(userId);
					duList.remove(j);
				}
			}
		}
	}
}

class DeptUserModel {
	private String userId;
	private String groupName;
	public DeptUserModel(String userId, String groupName) {
		super();
		this.userId = userId;
		this.groupName = groupName;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getGroupName() {
		return groupName;
	}
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}
	@Override
	public String toString() {
		return "DeptUserModel [userId=" + userId + ", groupName=" + groupName
				+ "]";
	}
	
}



处理前:
DeptUserModel [userId=00001, groupName=A]
DeptUserModel [userId=00002, groupName=C]
DeptUserModel [userId=00003, groupName=A]
DeptUserModel [userId=00023, groupName=C]
DeptUserModel [userId=00061, groupName=B]
处理后:
DeptUserModel [userId=00001,00003, groupName=A]
DeptUserModel [userId=00002,00023, groupName=C]
DeptUserModel [userId=00061, groupName=B]
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics