java list合并相同数据为一条数据

2021-08-14 00:00:00 java 数据 合并
//去除重复值
private IPage<SysLogNewsVO> getNewList(IPage<SysLogNewsVO> page,List<SysLogNewsVO> oldList){
	HashMap<String,SysLogNewsVO> tempMap = new HashMap<String,SysLogNewsVO>();
	List<SysLogNewsVO> currentPageList = new ArrayList<>();
	StringBuffer readUser=new StringBuffer();
	StringBuffer unReadUser=new StringBuffer();
	//去掉重复的key
	for(SysLogNewsVO vo : oldList){
		String temp = vo.getNewsTitle();
		if(tempMap.containsKey(temp)){
			SysLogNewsVO newPerson = new SysLogNewsVO();
			newPerson.setNewsTitle(tempMap.get(temp).getNewsTitle());
			newPerson.setNewsRecord(tempMap.get(temp).getNewsRecord());
			newPerson.setNewsType(tempMap.get(temp).getNewsType());
			newPerson.setTriggerModel(tempMap.get(temp).getTriggerModel());
			newPerson.setSendUser(tempMap.get(temp).getSendUser());
			newPerson.setSendTime(tempMap.get(temp).getSendTime());
			//合并相同key的value
			if(vo.getIsRead().equals("1")){
				readUser.append(vo.getReceiveUser()+',');
			}else if(vo.getIsRead().equals("0")){
				unReadUser.append(vo.getReceiveUser()+',');
			}
			newPerson.setReadPerson(readUser.toString());
			newPerson.setUnReadPerson(unReadUser.toString());
			newPerson.setReceiveUser(readUser.toString()+unReadUser.toString());
			tempMap.put(temp,newPerson );
		}else{
			readUser=new StringBuffer();
			unReadUser=new StringBuffer();
			if(vo.getIsRead().equals("1")){
				readUser.append(vo.getReceiveUser()+',');
				vo.setReadPerson(readUser.toString());
			}else if(vo.getIsRead().equals("0")){
				unReadUser.append(vo.getReceiveUser()+',');
				vo.setUnReadPerson(unReadUser.toString());
			}
			tempMap.put(temp,vo );
		}
	}
	//去除重复key的list
	List<SysLogNewsVO> newList = new ArrayList<SysLogNewsVO>();
	for(String temp:tempMap.keySet()){
		newList.add(tempMap.get(temp));
	}
	//分页
	if (newList != null && newList.size() > 0) {
		int size=newList.size();
		int pageStart=(int)(page.getCurrent()==1?0:(page.getCurrent()-1)*page.getSize());//截取的开始位置
		int pageEnd=(int)(size<page.getCurrent()*page.getSize()?size:page.getCurrent()*page.getSize());//截取的结束位置
		if(size>pageStart){
			currentPageList =newList.subList(pageStart, pageEnd);
		}
		//总页数
		int totalPage=(int)(newList.size()/page.getSize());
		page.setTotal(size);
	}
	return page.setRecords(currentPageList);
}
    原文作者:唯他命
    原文地址: https://blog.csdn.net/lslsllsl/article/details/123707049
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章