SpingMVC框架中去掉List中重複bean的方法

譚超zhong發表於2019-04-19

1.重寫實體類Bean的equals()和hashCode()方法

package com.wsbs.modules.personal.entity;

import com.ect.cloud.common.persistence.DataEntity;

/**
 * 
 * 此類描述的是:個人基本資訊
 */
public class PersonalBasicListBean extends DataEntity<PersonalBasicListBean>{

	private static final long serialVersionUID = 1L;
	private String grbh;//個人編號
	private String dwmc;//單位名稱
	private String sfzh;//身份證號
	private String xm;//姓名
	private String xb;//性別
	private String mz;//民族
	private String cjgzsj;//參加工作日期
	private String ryzt;//人員狀態
	private String cbzt;//參保狀態
	private String ssqh;//所屬區號
	private String kh;//社會保障卡卡號
	private String ltxsj;//離退休日期
	private String lxdh;//聯絡電話
	
	private String ybcnsj;//醫保參保時間
	private String ybjfys;//醫保繳費月數
	private String ybzhye;//醫保賬戶餘額
	private String ylcnsj;//養老參保時間
	private String yljfsj;//養老月數
	private String yljfys;//養老賬戶總額
	private String aab324;//統籌區 341801

	@Override //重寫equals方法 
	public boolean equals(Object obj) {  
	PersonalBasicListBean s=(PersonalBasicListBean)obj;   
	return grbh.equals(s.grbh) && 
		   dwmc.equals(s.dwmc) &&
		   sfzh.equals(s.sfzh) &&
		   xm.equals(s.xm) &&
		   xb.equals(s.xb) &&
		   mz.equals(s.mz) &&
		   cjgzsj.equals(s.cjgzsj) &&
		   ryzt.equals(s.ryzt) &&
		   cbzt.equals(s.cbzt) &&
		   ssqh.equals(s.ssqh) &&
		   kh.equals(s.kh) &&
		   ltxsj.equals(s.ltxsj) &&
		   lxdh.equals(s.lxdh) &&
		   ybcnsj.equals(s.ybcnsj) &&
		   ybjfys.equals(s.ybjfys) &&
		   ybzhye.equals(s.ybzhye) &&
		   ylcnsj.equals(s.ylcnsj) &&
		   yljfsj.equals(s.yljfsj) &&
		   yljfys.equals(s.yljfys);   
	}  
	
	@Override  //重寫hashCode方法
	public int hashCode() {  
	String in = grbh + dwmc + sfzh + xm + xb + mz+
			    cjgzsj + ryzt+ cbzt + ssqh + kh + ltxsj+ 
			    lxdh + ybcnsj+ybjfys + ybzhye+ ylcnsj + yljfsj+yljfys;  
	return in.hashCode();  
	}  

	
	public String getGrbh() {
		return grbh;
	}

	public void setGrbh(String grbh) {
		this.grbh = grbh;
	}

	public String getDwmc() {
		return dwmc;
	}

	public void setDwmc(String dwmc) {
		this.dwmc = dwmc;
	}

	public String getSfzh() {
		return sfzh;
	}

	public void setSfzh(String sfzh) {
		this.sfzh = sfzh;
	}

	public String getXm() {
		return xm;
	}

	public void setXm(String xm) {
		this.xm = xm;
	}

	public String getXb() {
		return xb;
	}

	public void setXb(String xb) {
		this.xb = xb;
	}

	public String getMz() {
		return mz;
	}

	public void setMz(String mz) {
		this.mz = mz;
	}

	public String getCjgzsj() {
		return cjgzsj;
	}

	public void setCjgzsj(String cjgzsj) {
		this.cjgzsj = cjgzsj;
	}

	public String getRyzt() {
		return ryzt;
	}

	public void setRyzt(String ryzt) {
		this.ryzt = ryzt;
	}

	public String getCbzt() {
		return cbzt;
	}

	public void setCbzt(String cbzt) {
		this.cbzt = cbzt;
	}

	public String getSsqh() {
		return ssqh;
	}

	public void setSsqh(String ssqh) {
		this.ssqh = ssqh;
	}

	public String getKh() {
		return kh;
	}

	public void setKh(String kh) {
		this.kh = kh;
	}

	public String getLtxsj() {
		return ltxsj;
	}

	public void setLtxsj(String ltxsj) {
		this.ltxsj = ltxsj;
	}

	public String getLxdh() {
		return lxdh;
	}

	public void setLxdh(String lxdh) {
		this.lxdh = lxdh;
	}

	public String getYbcnsj() {
		return ybcnsj;
	}

	public void setYbcnsj(String ybcnsj) {
		this.ybcnsj = ybcnsj;
	}

	public String getYbjfys() {
		return ybjfys;
	}

	public void setYbjfys(String ybjfys) {
		this.ybjfys = ybjfys;
	}

	public String getYbzhye() {
		return ybzhye;
	}

	public void setYbzhye(String ybzhye) {
		this.ybzhye = ybzhye;
	}

	public String getYlcnsj() {
		return ylcnsj;
	}

	public void setYlcnsj(String ylcnsj) {
		this.ylcnsj = ylcnsj;
	}

	public String getYljfsj() {
		return yljfsj;
	}

	public void setYljfsj(String yljfsj) {
		this.yljfsj = yljfsj;
	}

	public String getYljfys() {
		return yljfys;
	}

	public void setYljfys(String yljfys) {
		this.yljfys = yljfys;
	}

	public String getAab324() {
		return aab324;
	}

	public void setAab324(String aab324) {
		this.aab324 = aab324;
	}

}

2.用此bean接收實體類List集合接收資料,用hashSet方法進行去重得到新集合,並對新集合進行測試


//用list<Bean>接受資料
List<PersonalBasicListBean> pbList = 
                personalBasicService.getGrjbxxList(login.getUuid(), login.getAac002(), 
                login.getAac003(), Global.getConfig("token"));
//使用hashSet進行物件中元素的去重,獲得新物件集合list
Set<PersonalBasicListBean> ts = new HashSet<PersonalBasicListBean>();  
ts.addAll(pbList);  
//迴圈列印測試
System.out.println("迴圈列印出不重複的物件.........");
for (PersonalBasicListBean pbl : ts) {  
	ystem.out.println(pbl);  
} 
//將去重後的List集合放入檢視中
model.addAttribute("personalBasicBeanList", ts);

3.在jsp頁面上對List<Bean>集合進行長度判斷和迴圈

<c:forEach items="${personalBasicBeanList}" var="personalBasicBean">
	        <c:if test="${fn:length(personalBasicBeanList)>1}">
            <span style="font-weight:bold;color:green;font-size:14px">&nbsp;${personalBasicBean.xm}</span>
	        <span style="color:#157ab5;font-size:14px;font-weight:bold">在參保區&nbsp;</span>
	        <span style="font-weight:bold;color:green;font-size:14px">${fns:getDictLabel(personalBasicBean.ssqh, "aab324", "")}&nbsp;</span>
	        <span style="font-weight:bold;color:#157ab5;font-size:14px">的個人基本資訊</span>
	        </c:if>
	        
	        <table class="table table-striped table-bordered table-condensed">
			<tr>
				<td align="right"><label>身份證號:</label></td>
				<td>
					${personalBasicBean.sfzh}
				</td>
				<td align="right"><label>姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名:</label></td>
				<td>
					${personalBasicBean.xm}
				</td>
			</tr>
			<tr>
				<td align="right"><label>社保卡號:</label></td>
				<td>
					${personalBasicBean.kh}
				</td>
				<td align="right"><label>個人編號:</label></td>
				<td>
					${personalBasicBean.grbh}
				</td>
			</tr>
			<tr>
				<td align="right"><label>單位名稱:</label></td>
				<td>
					${personalBasicBean.dwmc}
				</td>
				<td align="right"><label>所屬縣區:</label></td>
				<td>
					${fns:getDictLabel(personalBasicBean.ssqh, "aab324", "")}
				</td>
			</tr>
			<tr>
				<td align="right"><label>人員狀態:</label></td>
				<td>
					${fns:getDictLabel(personalBasicBean.ryzt, "ryzt", "")}
				</td>
				<td align="right"><label>參保狀態:</label></td>
				<td>
					${fns:getDictLabel(personalBasicBean.cbzt, "cbzt", "")}
				</td>
			</tr>
			<tr>
				<td align="right"><label>離退休時間:</label></td>
				<td>
					${personalBasicBean.ltxsj}
				</td>
				 <td align="right"><label>聯絡電話:</label></td>
				<td>
					${personalBasicBean.lxdh}
				</td> 
			</tr>
			<tr>
				<td align="right"><label>醫保參保時間:</label></td>
				<td>
					${personalBasicBean.ybcnsj}
				</td>
				 <td align="right"><label>醫保繳費月數:</label></td>
				<td>
					${personalBasicBean.ybjfys}
				</td> 
			</tr>
			<tr>
				<td align="right"><label>醫保賬戶餘額:</label></td>
				<td>
					${personalBasicBean.ybzhye}
				</td>
				 <td align="right"><label>養老參保時間:</label></td>
				<td>
					${personalBasicBean.ylcnsj}
				</td> 
			</tr>
			<tr>
				<td align="right"><label>養老月數:</label></td>
				<td>
					${personalBasicBean.yljfsj}
				</td>
				 <td align="right"><label>養老賬戶總額:</label></td>
				<td>
					${personalBasicBean.yljfys}
				</td> 
			</tr>
		</table>
	    <hr>
	    </c:forEach>

 

相關文章